我在 LinearLayout 中有两个 TextView。布局方向为水平方向。我必须保持两个 Textview 高度相等。但根据文本长度,两者以不同的高度显示。
我曾尝试使文本长度如下所示
public static String padRight(String s, int n) {
return String.format("%1$-" + n + "s", s);
}
但是高度有点不同。我该如何解决这个问题?
最佳答案
如果我没有误解您的意思,您希望将 TextView
放在水平方向的 LinearLayout
中,并且两个 TextView
必须具有相同的高度.
然后使用如下所示的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#B40431"
android:text="text sample\ntext sample\ntext sample\ntext sample\ntext sample\ntext sample\n"
android:gravity="center_vertical"/>
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0000FF"
android:text="text sample\ntext sample\ntext sample"
android:gravity="center_vertical"/>
</LinearLayout>
输出将是这样的:
关于android - 在LinearLayout中保持textview的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19873468/