簡體   English   中英

父級LinearLayout不限制將內部TextView的大小設置為WRAP_CONTENT

[英]Parent LinearLayout Not Limiting Size of Inner TextView Set To WRAP_CONTENT

我將LinearLayout設置為100dp的靜態高度。 然后,在其中有一個textView,將其高度設置為WRAP_CONTENT。 問題是當textView的高度大於100dp時,textView導致LinearLayout完全擴展以容納textView

之所以設置它,是因為我希望能夠在用戶單擊按鈕時顯示和隱藏全文。 我認為將linearlayout的高度設置為一些最小值,然后在單擊時將其更新為WRAP_CONTENT即可,但是當我嘗試限制高度時,LinearLayout會忽略它。 我試圖將clipChildren設置為true,但這沒有幫助。 代碼如下。 我正在使用Android Annotations,但我認為這與問題無關。

<ScrollView
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="match_parent"
android:padding="30dp"
android:fitsSystemWindows="true"
android:background="@color/light_grey">

<LinearLayout
    android:orientation="vertical"
    android:clipChildren="true"
    android:background="@color/white"
    android:layout_width="match_parent"
    android:layout_height="100dp">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    </LinearLayout>

和Java代碼:

@EActivity(R.layout.activity_test)
public class TestActivity extends AppCompatActivity {

@ViewById(R.id.text_view)
TextView textView;

String input;
public static final String INPUT_STRING_KEY = "inputStringKeyForParser";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.input = this.getIntent().getExtras().getString(INPUT_STRING_KEY);
}

@AfterViews
public void showText() {
    textView.setText(input);
}

}

有誰知道如何使TextView停止導致線性布局擴展? 謝謝!

嘗試

<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="100dp" />

好。 我認為您不能以這種方式應用height屬性,因為您的布局是ScrollView的子級。 將您的TextView放在另一個LinearLayout中,如下所示:

<ScrollView
    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="match_parent"
    android:padding="30dp"
    android:fitsSystemWindows="true"
    android:background="@color/light_grey">

    <LinearLayout
        android:orientation="vertical"
        android:clipChildren="true"
        android:background="@color/white"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="100dp">

            <TextView
                android:id="@+id/text_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />
        </LinearLayout>

    </LinearLayout>
</ScrollView>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM