嵌套线性布局仅显示在Android中设置为可见后的第一个视图

问题描述:

我正在开发Android应用程序,但我仍然很新。我想要一个按钮,当你按下这个按钮时,会出现几个TextViews和Buttons。所以我有一个主要的线性布局,然后嵌入另一个线性布局,里面包含我想隐藏的东西。我有嵌套的线性布局设置为android:visibility =“gone”。嵌套线性布局仅显示在Android中设置为可见后的第一个视图

我遇到的问题是,它只显示隐藏的线性布局内的第一项而不是全部。我试图让它出现的方式是

vgAddView = (ViewGroup)findViewById(R.id.add_details); 

    btnAche.setOnClickListener(new OnClickListener(){ 
     public void onClick(View v){ 
      vgAddView.setVisibility(0); 
     } 
    }); 

我的XML文件是这样的

<?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="wrap_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> 

TextViewLinearLayout设置为android:layout_width="fill_parent"android:layout_height="fill_parent",这意味着它会占用整个空间LinearLayout,留下Button没有空间。如果您使用SDK附带的hierarchyviewer工具,则可以在查看活动时看到。

您需要将TextView的高度设置为wrap_content或以其他方式为Button留出空间。

你喜欢低于其可见性设置为true集后:

  • vgAddView.setVisibility(View.VISIBLE); - 显示它
  • vgAddView.setVisibility(View.GONE); - 隐藏它

你先TextView中具有layout_width =“FILL_PARENT ”。 这意味着,你的TextView不会留下Button的空间。 您可以尝试设置layout_weight属性或将layout_width设置为wrap_content