我希望TextView高度根据它包含的文本数量自动重新设置。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/red"
android:gravity="center"
android:text="COOK COMMENT"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:background="@color/white"
android:orientation="vertical" >
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:text="@string/longtext"
android:textColor="@color/black" />
</LinearLayout>
</LinearLayout>
例如,当textView2应该自动缩放而不是linearLayout高度应该改变它。
答案 0 :(得分:1)
使用android:layout_height="wrap_content"
并确保将TextView
嵌套在ScrollView
内。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/red"
android:gravity="center"
android:text="COOK COMMENT"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:background="@color/white"
android:orientation="vertical" >
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:text="@string/longtext"
android:textColor="@color/black" />
</ScrollView>
</LinearLayout>
答案 1 :(得分:0)
将以下代码添加到控制视图的Activity:
textView2.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
v.setLines(v.getLineCount());
return true;
}
});
您还可以自定义代码,以行单位设置最小或最大高度。