Android - 布局不会出现在设备上,因为它出现在预览中

时间:2017-06-05 18:57:54

标签: android xml layout

这是布局的XML文件的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.barda.wikirace.MainActivity"
    android:background="@drawable/backgroundmain"
    android:orientation="vertical"
    android:weightSum="1"
    android:id="@+id/activity_main"
    >


    <ImageView
        android:id="@+id/wikiRaceTop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@android:color/transparent"
        app:srcCompat="@drawable/wikiracetop" />

    <TextView
        android:id="@+id/emptyView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
         />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1"
        >
        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.25"/>

        <Button
            android:layout_marginBottom="7dp"
            android:id="@+id/twoPlayersBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/twoplayersbutton"
            />

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"/>




        <Button
            android:layout_marginBottom="7dp"
            android:id="@+id/singlePlayerBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/playbutton"
            android:onClick="startSinglePlayerMenuActivity"/>
        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.25"/>



    </LinearLayout>


</LinearLayout>

这就是它在预览中的显示效果: enter image description here

这就是它在设备上的显示方式(LG G4): enter image description here

可能导致什么问题? 如您所见,我希望按钮的颜色正确。

1 个答案:

答案 0 :(得分:0)

为什么不只使用约束布局? 如果你想以你的方式做到这一点,你必须设置宽度固定值或match_parent为什么?因为当你说wrap_content并且图像很大时会发生其他视图,你也可以为按钮设置一个android:layout_weight,如果你想使用权重作为你的布局的“控制”,那就是与用于调整视图的方法。