线性布局的重量无法在手机上工作,在android studio的xml预览中工作正常

问题描述 投票:1回答:5

我在我的xml文件中使用了LinearLayout中的layout_weight。预览很好,但是当我在手机中运行应用程序时,视图是不同的。这是xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

        <!--<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/title_dashboard"
            android:textSize="25sp"/>-->

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="200dp">

        <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="16dp">

        <ImageView
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:id="@+id/ivBrandImage"
            android:scaleType="centerCrop"
            android:src="@mipmap/ic_launcher"
            android:layout_centerVertical="true"/>


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:id="@+id/tBrandName"
            android:layout_toRightOf="@+id/ivBrandImage"
            android:text="Email name"
            android:layout_marginLeft="@dimen/margin_left_brand_name"
            android:layout_centerVertical="true"/>

        <android.support.v7.widget.SwitchCompat
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tBrandName"
            android:layout_toRightOf="@+id/ivBrandImage"
            android:layout_marginLeft="@dimen/margin_left_brand_name"
            android:layout_marginTop="6dp"
            android:id="@+id/switchBrand"/>

    </RelativeLayout>

    </android.support.v7.widget.CardView>


    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_marginTop="@dimen/margin_top_dashboard_card"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:layout_marginLeft="@dimen/margin_left_dashboard"
            android:text="@string/textview_pending_approvals"
            />

    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="@dimen/margin_top_dashboard_card"
        android:layout_weight="1"
        android:layout_gravity="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/margin_left_dashboard"
            android:text="@string/textview_pending_approvals"
            android:gravity="center_vertical"
            />

    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="@dimen/margin_top_dashboard_card"
        android:layout_weight="1"
        android:layout_gravity="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/margin_left_dashboard"
            android:text="@string/textview_pending_approvals"
            android:gravity="center_vertical"
            />

    </android.support.v7.widget.CardView>

</LinearLayout>

以下是xml预览:

这是手机上的实际屏幕截图:

我无法弄清楚,为什么实际屏幕与xml预览不同。有人可以告诉我这里的错误是什么?

android android-linearlayout android-xml
5个回答
0
投票

请为hdpi,xhdip,xxhdpi,xxxhdpi等所有分辨率制作。请从此处选择您测试的设备分辨率,然后再次测试。

enter image description here


0
投票
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <!--<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/title_dashboard"
        android:textSize="25sp"/>-->

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="200dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="16dp">

            <ImageView
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:id="@+id/ivBrandImage"
                android:scaleType="centerCrop"
                android:src="@mipmap/ic_launcher"
                android:layout_centerVertical="true"/>


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="30sp"
                android:id="@+id/tBrandName"
                android:layout_toRightOf="@+id/ivBrandImage"
                android:text="Email name"
                android:layout_marginLeft="@dimen/button_padding"
                android:layout_centerVertical="true"/>

            <android.support.v7.widget.SwitchCompat
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/tBrandName"
                android:layout_toRightOf="@+id/ivBrandImage"
                android:layout_marginLeft="@dimen/button_padding"
                android:layout_marginTop="6dp"
                android:id="@+id/switchBrand"/>

        </RelativeLayout>

    </android.support.v7.widget.CardView>


    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_marginTop="@dimen/button_padding"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:layout_marginLeft="@dimen/button_padding"
            android:text="fjdkjk"
            />

    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="@dimen/button_padding"
        android:layout_weight="1"
        android:layout_gravity="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/button_padding"
            android:text="jdfkjffjkjk"
            android:gravity="center_vertical"
            />

    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="@dimen/button_padding"
        android:layout_weight="1"
        android:layout_gravity="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/button_padding"
            android:text="jkgjk"
            android:gravity="center_vertical"
            />

    </android.support.v7.widget.CardView>

</LinearLayout>

导入它 -

编译'com.android.support:cardview-v7:21.0.+'

军情四处工作正常


0
投票

请尝试以下方法:

使用android:layout_height="0dp"android:layout_weight="1"作为第一个CardView(其中包含Android徽标的图像)。


0
投票

将xml文件的父布局从LinearLayut更改为RelativeLayout解决了这个问题。当然,为实现这一目标需要进行一些细微的改变。这是xml文件,它在手机上运行正常。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_alignParentTop="true"
        android:id="@+id/cvDashboardHeader">

        <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="16dp">

            <ImageView
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:id="@+id/ivBrandImage"
                android:scaleType="centerCrop"
                android:src="@mipmap/ic_launcher"
                android:layout_centerVertical="true"
                android:foreground="?android:attr/selectableItemBackground"
                android:clickable="true"/>


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="30sp"
                android:id="@+id/tBrandName"
                android:layout_toRightOf="@+id/ivBrandImage"
                android:text="Email name"
                android:layout_marginLeft="@dimen/margin_left_brand_name"
                android:layout_centerVertical="true"/>

            <android.support.v7.widget.SwitchCompat
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/tBrandName"
                android:layout_toRightOf="@+id/ivBrandImage"
                android:layout_marginLeft="@dimen/margin_left_brand_name"
                android:layout_marginTop="6dp"
                android:id="@+id/switchBrand"/>

        </RelativeLayout>

    </android.support.v7.widget.CardView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_below="@+id/cvDashboardHeader">

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_marginTop="@dimen/margin_top_dashboard_card"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center_vertical"
                android:layout_marginLeft="@dimen/margin_left_dashboard"
                android:text="@string/textview_pending_approvals"
                />

        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="@dimen/margin_top_dashboard_card"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="@dimen/margin_left_dashboard"
                android:text="@string/textview_pending_approvals"
                android:gravity="center_vertical"
                />

        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="@dimen/margin_top_dashboard_card"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="@dimen/margin_left_dashboard"
                android:text="@string/textview_pending_approvals"
                android:gravity="center_vertical"
                />

        </android.support.v7.widget.CardView>

    </LinearLayout>

</RelativeLayout>

0
投票

如果是LinearLayout。它是通过以下代码进行演示。您可以解决您的问题。使用以下代码。

  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10sp"
        android:weightSum="3">

        <TextView
            android:id="@+id/text_kpis"
            android:layout_width="0sp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:gravity="center"
            android:textColor="@color/text_color_dark"
            android:textSize="@dimen/text_size_13" />


        <TextView
            android:id="@+id/text_target"
            android:layout_width="0sp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:textColor="@color/txtbox_text_color_darek"
            android:textSize="@dimen/text_size_13" />


        <TextView
            android:id="@+id/text_actual"
            android:layout_width="0sp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:textColor="@color/txtbox_text_color_darek"
            android:textSize="@dimen/text_size_13" />
    </LinearLayout>

如果是相对布局使用下面的代码。

 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:padding="10sp"
        android:textColor="@android:color/white"
        >

        <TextView
            android:id="@+id/tv_kpis"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/kpi"
            android:layout_alignParentLeft="true"
            android:textColor="@android:color/white"
            android:textSize="@dimen/text_size_16" />

        <TextView
            android:id="@+id/tv_target"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="@string/traget"
            android:textColor="@android:color/white"
            android:textSize="@dimen/text_size_16" />


        <TextView
            android:id="@+id/tv_actual"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:text="@string/actual"
            android:textColor="@android:color/white"
            android:textSize="@dimen/text_size_16" />


    </RelativeLayout>
© www.soinside.com 2019 - 2024. All rights reserved.