我正在尝试连续执行 3 个 TextView。我编写了一个示例,现在我有:
<RelativeLayout
android:id="@+id/relative"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:padding="5dp">
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textColor="#f00"
android:text="one"
android:layout_alignParentLeft="true" ></TextView>
<TextView
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textColor="#f00"
android:layout_alignParentLeft="true"
android:text="two" ></TextView>
<TextView
android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textColor="#f00"
android:text="three"
android:layout_alignParentRight="true"></TextView>
</RelativeLayout>
我想要:id:TextView01
在左边,id:TextView02
在中间,id:TextView03
在右边。
现在 id:TextView01
和 id:TextView03
工作正常,但是 id:TextView02
应用于 id:TextView01
.
我可以使用其他布局,但我希望它从左到右自动居中。
最佳答案
创建线性布局。使 TextView 与父 View 匹配并且布局权重相等以恰好占行的 1/3
<LinearLayout
android:id="@+id/relative"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:padding="5dp"
orientation="horizontal">
<TextView
android:id="@+id/TextView01"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textColor="#f00"
android:text="one"
></TextView>
<TextView
android:id="@+id/TextView02"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textColor="#f00"
android:layout_alignParentLeft="true"
android:text="two" ></TextView>
<TextView
android:id="@+id/TextView03"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textColor="#f00"
android:text="three"
android:layout_alignParentRight="true"></TextView>
</LinearLayout>
关于java - 一行三个textView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25325262/