相对于彼此对齐 TextView
Align TextViews relative to each other
我有两个TextView。
第一个 TextView 包含可小可大的动态文本。
第二个 TextView 包含静态小文本。
第二个 TextView 应位于第一个 TextView 的右侧。
它应该是这样工作的:
- 当第一个文本较大时:
- 当文本很小时:
如何制作这个布局?当第一个 TextView 包含大文本时使用相对或线性布局 - 第二个文本从屏幕上消失。
您可以添加方向为 "horizontal" 的 LinearLayout,并在其中添加两个文本视图。
<LinearLayout
...
android:orientation="horizontal">
<TextView .../>
<TextView .../>
</LinearLayout>
试试这个代码,我认为它会如你所愿
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<TextView
android:id="@+id/txt_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="asdasdadasdasdasdasdasdasdasdasdasdasdasdasd"/>
<TextView
android:layout_weight="1"
android:layout_width="wrap_content"
android:text="2asdasdadasdasdasdasdasdasdasdasdasdasdasdasd"
android:layout_height="wrap_content"/>
</LinearLayout>
试试这个 FlexboxLayout
将以下依赖项添加到您的 build.gradle 文件中:
dependencies {
compile 'com.google.android:flexbox:0.3.2'
}
用法
<com.google.android.flexbox.FlexboxLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:flexWrap="nowrap"
app:alignItems="stretch"
app:alignContent="stretch" >
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0"
android:text="niledfdfdfhfgfdgdfgdfgfgfggdfgdfggggggggggggggggggggggggggggggggggggggggggggggfhssdsdsdsfgdfgfgfgfgfggfgdgdfgfgfgdsddjfhdkjfhjshdf" />
<TextView
android:id="@+id/textview2"
android:layout_width="wrap_content"
android:text="nilxcvxceshdfhd"
android:background="@color/colorAccent"
android:layout_height="wrap_content"
app:layout_minWidth="50dp"
app:layout_minHeight="20dp"
app:layout_alignSelf="center"
/>
</com.google.android.flexbox.FlexboxLayout>
输出
大文本输出
我有两个TextView。 第一个 TextView 包含可小可大的动态文本。 第二个 TextView 包含静态小文本。 第二个 TextView 应位于第一个 TextView 的右侧。
它应该是这样工作的:
- 当第一个文本较大时:
- 当文本很小时:
如何制作这个布局?当第一个 TextView 包含大文本时使用相对或线性布局 - 第二个文本从屏幕上消失。
您可以添加方向为 "horizontal" 的 LinearLayout,并在其中添加两个文本视图。
<LinearLayout
...
android:orientation="horizontal">
<TextView .../>
<TextView .../>
</LinearLayout>
试试这个代码,我认为它会如你所愿
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<TextView
android:id="@+id/txt_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="asdasdadasdasdasdasdasdasdasdasdasdasdasdasd"/>
<TextView
android:layout_weight="1"
android:layout_width="wrap_content"
android:text="2asdasdadasdasdasdasdasdasdasdasdasdasdasdasd"
android:layout_height="wrap_content"/>
</LinearLayout>
试试这个 FlexboxLayout
将以下依赖项添加到您的 build.gradle 文件中:
dependencies {
compile 'com.google.android:flexbox:0.3.2'
}
用法
<com.google.android.flexbox.FlexboxLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:flexWrap="nowrap"
app:alignItems="stretch"
app:alignContent="stretch" >
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0"
android:text="niledfdfdfhfgfdgdfgdfgfgfggdfgdfggggggggggggggggggggggggggggggggggggggggggggggfhssdsdsdsfgdfgfgfgfgfggfgdgdfgfgfgdsddjfhdkjfhjshdf" />
<TextView
android:id="@+id/textview2"
android:layout_width="wrap_content"
android:text="nilxcvxceshdfhd"
android:background="@color/colorAccent"
android:layout_height="wrap_content"
app:layout_minWidth="50dp"
app:layout_minHeight="20dp"
app:layout_alignSelf="center"
/>
</com.google.android.flexbox.FlexboxLayout>
输出
大文本输出