Android 如何根据用户选择更改布局元素
我有一个相对布局的活动Android 如何根据用户选择更改布局元素,android,Android,我有一个相对布局的活动 <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"> &l
<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个文本视图或4个按钮1个文本视图。因此,为此,我必须使用xml布局中的所有元素,并根据用户选择使用“android:visibility”可选来显示,或者有其他更好的方法来显示,因为我发现这个方法有点混乱
谢谢这是您使用Android的方式:可视性可以让它变得更简单。您可以为微调器中的每个项目创建多个线性布局。请注意,我假设其中的项目数是静态的。将布局命名为布局0、布局1、布局2。。。对应于微调器中的每个项目。然后根据微调器的选择更改可见性
另一种方法是使用布局充气器,并通过代码动态填充视图。使用Android:visibility的方法可以使其更简单。您可以为微调器中的每个项目创建多个线性布局。请注意,我假设其中的项目数是静态的。将布局命名为布局0、布局1、布局2。。。对应于微调器中的每个项目。然后根据微调器的选择更改可见性
另一种方法是使用布局充气器并通过代码动态填充视图。我更喜欢像Pravin said那样使用充气器,我更喜欢像Pravin说的那样使用充气器