简体   繁体   中英

Parent LinearLayout Not Limiting Size of Inner TextView Set To WRAP_CONTENT

I have a LinearLayout set to a static height of 100dp. I then have a textView inside it on which I set height to WRAP_CONTENT. The problem is the textView is causing the LinearLayout to expand fully to accommodate the textView when the height of the textView is greater than 100dp

I set it up this way because I want to be able to show and hide the full text when the user clicks a button. I thought setting the linearlayout's height to some minimum and then updating it to WRAP_CONTENT when clicked would work, but when I try to limit the height, the LinearLayout ignores it. I tried to set clipChildren to true but that didn't help. Code is below. I am using Android Annotations, but I don't think that's related to the problem.

<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>

And the Java code:

@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);
}

}

Does anyone know how to get the TextView to stop causing the linearlayout to expand? Thank you!

Try

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

Ok. I think that you can't apply height property this way because your layout is the child of ScrollView. Put your TextView inside another LinearLayout like this:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM