【发布时间】:2016-07-22 07:19:47
【问题描述】:
我正在开发一个 Android Studio 项目。我想在屏幕底部添加一个Fragment。到目前为止一切顺利,它可以工作并显示出来。但是当我想在顶部显示RelativeLayout 时,它不会显示在模拟器上,而是显示在 Android Studio 的设备屏幕上。我需要布局仅在屏幕顶部创建背景。我的代码是否有错误,或者有其他方法可以创建背景?
这是我的 XML 代码:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
android:background="#000">
<fragment
android:id="@+id/fragment1"
android:name="com.htlhl.listfragment.MyListFragment"
android:layout_width="match_parent"
android:layout_height="197dp"
android:layout_alignParentBottom="true" />
<!--The following RelativeLayout is not shown at the top-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/relativeLayout"
android:background="#00632e"
android:focusable="true">
<TextView
android:layout_width="250dp"
android:layout_height="30dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Medium Text"
android:gravity="center"
android:textColor="#000"
android:background="#fff"
android:id="@+id/textView2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/buttonBarButtonStyle"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:text="Start"
android:textColor="#fff"/>
</RelativeLayout>
</RelativeLayout>
Android 工作室:
模拟器和真机:
【问题讨论】:
-
您能否提供来自 Android Studio 和设备的屏幕截图?您可以编辑您的帖子以添加更多信息。
-
我尝试在真实设备(Moto G 2)上运行它并且它在其中运行良好。只是不要总是依赖模拟器,尝试在真实设备上进行测试。
-
谢谢你 Maniya Joe,我会试试的。如果它不起作用,我将发布 Aleksandar 的屏幕截图
-
在真实设备上也没有显示。我添加了所有截图。 @AleksandarStefanović
-
尝试将RelativeLayout 移动到片段上方(在XML 中),以便RelativeLayout 首先出现在XML 文本中,然后是片段。我不是 100% 肯定这是问题所在,但值得一试。另外,你为什么使用RelativeLayout而不是LinearLayout?有什么具体原因吗?
标签: android xml android-layout background android-relativelayout