RelativeLayout显示在AndroidStudio的Devicescreen上,但不显示在Emulator上

时间:2016-07-22 07:19:48

标签: android xml android-layout background relativelayout

我正在开发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 Studio:

Android Studio

模拟器和真实设备:

Emulator

2 个答案:

答案 0 :(得分:0)

试试这个:

<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>

我可以进一步改进你的布局,告诉我这是否有效。

答案 1 :(得分:0)

force clean workspace

关于TextView ......