【问题标题】:RelativeLayout is shown on the Devicescreen of AndroidStudio but not on EmulatorRelativeLayout 显示在 Android Studio 的设备屏幕上,但不显示在模拟器上
【发布时间】: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


【解决方案1】:

试试这个:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#000">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        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>

    <fragment
        android:id="@+id/fragment1"
        android:name="com.htlhl.listfragment.MyListFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

我可以进一步改进您的布局,请告诉我这是否可行。

【讨论】:

  • 谢谢,但现在我真的发现了错误,在我的 content_main.xml 文件中,布局位于顶部。 content_main.xml 包含在 activity_main.xml -> 但包含从屏幕顶部开始,而不是在工具栏下方,因此我正在搜索的布局位于工具栏后面。如何将 iclude 移动到工具栏下方?
  • 如果 的父布局是 CoordinatorLayout,它自己处理它(问题应该不存在)。我猜,既然你已经将尺寸设置为静态 DP,可能你的手机上没有足够的空间来显示所有元素。
  • @Goetti,如果你还没有,试试我的布局,值得一试,也许它会解决你的问题。
  • 如果我将它设置为 70dp,我可以看到大约 15dp,所以 55dp 在后面工具栏...在我的另一个项目中,包含从工具栏下方开始,在这个项目中,我最终想使用它。 .所以他们应该不是问题
  • 然后尝试将整个 标签移动到 XML 中的工具栏下方(只需移动下面的文本)。
【解决方案2】:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:background="#00632e"
android:baselineAligned="false"
android:weightSum="1.0">

<LinearLayout
    android:layout_width="0dp"
    android:layout_weight="0.8"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical">
    <TextView
        android:layout_width="200dp"
        android:layout_height="30dp"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Medium Text"
        android:gravity="center"
        android:layout_gravity="center_vertical"
        android:textColor="#000"
        android:background="#fff"
        android:id="@+id/textView2" />
</LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.2">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="?android:attr/buttonBarButtonStyle"
            android:text="Start"
            android:gravity="center"
            android:textColor="#fff" />
    </LinearLayout>

</LinearLayout>

关于 TextView...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 2014-05-03
    相关资源
    最近更新 更多