我创建了一个带有切换按钮的 Activity 。我还编写了处理点击的代码。
但是当我运行应用程序时,切换按钮不会显示在 Activity 屏幕上。
我认为可能是模拟器的问题,所以我将其导出并在手机上尝试。
但它也不会显示在手机上。
这是 Activity 布局文件描述。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/btnStartService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Start Notifying" />
<EditText
android:id="@+id/txtUserMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btnStartService"
android:layout_centerHorizontal="true"
android:layout_marginBottom="82dp"
android:ems="10"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/txtWelcomeMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="TextView" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/txtUserMsg"
android:layout_centerHorizontal="true"
android:text="Message to reply with" />
<Button
android:id="@+id/stopNotifying"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnStartService"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="Stop Notifying" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btnStartService"
android:layout_alignLeft="@+id/txtUserMsg"
android:layout_marginBottom="29dp"
android:text="Caller Notifications" />
<ToggleButton
android:id="@+id/tglStartStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_toRightOf="@+id/txtWelcomeMsg"
android:text="ToggleButton"
android:visibility="visible" />
</RelativeLayout>
知道为什么会这样吗?
最佳答案
我认为你有一个概念错误,当定位与其他小部件相关的小部件时,你应该这样做: 示例:
android:layout_toRightOf="@id/txtWelcomeMsg"
而不是:
android:layout_toRightOf="@+id/txtWelcomeMsg"
关于android - 切换按钮未显示在应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12695349/