Nested Linear layout only shows first view after being set from gone to visible in Android
I am developing an Android app but I'm still pretty new. I want to have a button, and when you push that button, a few TextViews and Buttons will appear. So I have a main linear layout, and then another linear layout nested inside containing the things I want hidden. I have the nested linear layout set to android:visibility="gone".
The problem I am having is that it only shows the first item inside the hidden linear layout instead of all of them. The way I try to make it appear is
vgAddView = (ViewGroup)findViewById(R.id.add_details);
btnAche.setOnClickListener(new OnClickListener(){
public void onClick(View v){
vgAddView.setVisibility(0);
}
});
My XML file is this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:text="@string/but_stomach_ache"
android:id="@+id/but_stomach_ache"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button
android:text="@string/but_food"
android:id="@+id/but_food"
android:layout_width="fill_parent"
android:layout_height="w开发者_如何学JAVArap_content">
</Button>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/add_details"
android:visibility="gone">
<TextView
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/when_happen">
</TextView>
<Button
android:text="@string/happen_now"
android:id="@+id/happen_now"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
</LinearLayout>
Your TextView
in the LinearLayout
is set to android:layout_width="fill_parent"
and android:layout_height="fill_parent"
, which means it will take up the entire space of the LinearLayout
, leaving no room for the Button
. If you use the hierarchyviewer
tool that shipped with the SDK, you can see that when you look at the activity.
You need to set the height of the TextView
to be wrap_content
or otherwise have it leave room for the Button
.
After you are setting its visibility to true set like below:
vgAddView.setVisibility(View.VISIBLE);
- to show itvgAddView.setVisibility(View.GONE);
- to hide it
You first TextView has a layout_width="fill_parent". It's mean,that you TextView don't leave space for Button. You may try to set layout_weight attribute or set layout_width to wrap_content
精彩评论