Java Android按钮更改视图
我有一个问题,我不能左右我的头,我是相当新的安卓系统,我正在努力与一个基本的概念,使用一个按钮来改变布局视图。我的活动也是如此Java Android按钮更改视图,java,android,Java,Android,我有一个问题,我不能左右我的头,我是相当新的安卓系统,我正在努力与一个基本的概念,使用一个按钮来改变布局视图。我的活动也是如此 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/bg"
android:orientation="vertical"
tools:context=".MainActivity" >
<DigitalClock
android:id="@+id/DigitalClock"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:textColor="#ffffff"
android:gravity="center"
android:textSize="70sp" />
<View
android:layout_width="fill_parent"
android:layout_height="3dp"
android:background="@color/line"
/>
<Button
android:id="@+id/AddAlarm"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/add_button"
/>
<Button
android:id="@+id/AddAlarm2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/add_button"
/>
我有点理解如何使用按钮点击来改变视图,但我显然做错了什么。这是我的add_alarm2布局代码。但是,当您单击imagebutton android:id=@+id/AddAlarm2时,它不会执行任何操作,而不会打开add_alarm2.xml视图。我们非常感谢您的帮助
这是我的add_alarm2.xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/bg"
tools:context=".MainActivity" >
<ImageButton
android:id="@+id/AddAlarm2"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@null"
android:src="@drawable/normal"
android:scaleType="fitCenter"
/>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@color/line"
/>
</LinearLayout>
一种快速而肮脏的方法是将要显示的视图放在activity_main.xml中,但可见性=消失。使用view.setVisibilityView.VISIBLE 然后,单击按钮时,将此元素的可见性切换为可见。setContentView在onCreate中只能使用一次,多次调用它可能会产生意外的结果
一个更好的方法是探索其他替代方法,如,它将使代码更干净。也许您应该使用ViewFlipper或ViewSwitcher。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/bg"
tools:context=".MainActivity" >
<ImageButton
android:id="@+id/AddAlarm2"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@null"
android:src="@drawable/normal"
android:scaleType="fitCenter"
/>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@color/line"
/>
</LinearLayout>