2011-06-13 75 views
1

我有一个具有相对布局的活动。如何根据用户选择更改布局元素

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="#FFFFFF"> 



<TextView android:id="@+id/time_label" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text = "Name :" 
     android:textSize = "14sp" 
     android:textColor = "#000000" 
     android:textStyle = "bold" 
     android:layout_gravity="center_vertical|center_horizontal" 
    /> 

    <Spinner android:id="@+id/spinner_time" 
     android:layout_height="40dp" 
     android:layout_width="match_parent" 
      android:prompt="@string/time_list" 
     android:layout_below = "@+id/time_label" 
     android:layout_marginLeft = "5dp" 
     android:layout_marginRight = "5dp"> 
    </Spinner> 

<TextView></TextView> 
<EditText></EditText> 
<ImageButton></ImageButton> 

</RelativeLayout> 

现在根据用户从微调选择我需要改变布局元素像2按钮3 textview或4按钮1 textview。所以对于这个我必须采取在xml布局的所有元素,并使用“android:知名度”可选显示根据用户选择或有任何其他更好的方式来做到这一点,因为我发现这种方法有点混乱。

感谢

回答

1

这是您要使用Android的方式:能见度可以使其更容易。你可以做的是为微调器中的每个项目创建多个linearLayouts。请注意,我假设其中的项目数量是静态的。将布局命名为与Spinner中的每个项目对应的layout0,layout1,layout2 ...。然后基于选择为微调改变能见度。

另一种方式是使用布局充气器并通过代码动态填充视图。

+0

我更喜欢使用充气器,因为Pravin说 – 2011-06-13 13:08:24

+0

谢谢,明白了。你能指点我一个包含基本代码的例子吗? – Rishi 2011-06-14 06:38:01