开发者

how to change layout elements according to user selection

i have an activity which has an relative layout.

<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_开发者_C百科marginLeft = "5dp"
         android:layout_marginRight = "5dp">
   </Spinner>

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

</RelativeLayout>

Now according to user selection from spinner i need to change the layout elements like 2 buttons 3 textview or 4 buttons 1 textview. So for this do i have to take all the elements in the xml layout and use "android:visibility" optional to show according to user selection or there is any other better way to do this cause i am finding this method a bit cluttered.

Thanks


It is the way you are going to use the Android:visibility that can make it easier. What you can do is create multiple linearLayouts for each item in spinner. Mind you I am assuming the number of items in it are static. Name the layouts as layout0, layout1, layout2 ... corresponding to each item in the Spinner. Then based on selection for spinner change the visibility.

The other way is to use layout inflater and populate the view dynamically through code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜