[英]Android layout looks different on different devices
我正在开发一个 Android 应用程序,现在我写了一个布局,它在不同的设备上看起来不同。
首先是它的样子:
正如我们在第一个上部(顶部到水平线)看到的那样,正确的权重 = 2,下部的权重 = 1。在第二张图片上,这些比例似乎是完全错误的。 为什么?
这是我的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="?actionBarSize"
android:weightSum="3">
<RelativeLayout
android:id="@+id/rl5"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:layout_marginBottom="5dp"
android:orientation="vertical"
android:paddingHorizontal="10dp"
>
<LinearLayout
android:id="@+id/LL1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/LL2"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_benchmarks"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#ffffff"
android:dividerHeight="1dp"
android:footerDividersEnabled="true"
android:scrollbars="vertical"
android:headerDividersEnabled="true" />
</LinearLayout>
<LinearLayout
android:id="@+id/LL2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/b_add_benchmark"
android:baselineAligned="false">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/input_layout_email"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/ben_hint_name"
android:inputType="text"
android:textSize="20sp" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/input_layout_email2"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1">
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/ben_hint_size"
android:inputType="number"
android:textSize="20sp" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/b_add_benchmark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="@drawable/button_selector"
android:padding="10dp"
android:text="@string/add_ben"
android:textSize="20sp"
tools:gravity="center" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginBottom="?attr/actionBarSize">
<View
android:id="@+id/View1"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="@dimen/margin_top_of_bar"
android:layout_marginBottom="5dp"
android:background="@android:color/darker_gray" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/View1">
<ImageView
android:id="@+id/iv7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.2"
android:src="@drawable/ic_ben_measure"
android:scaleType="fitXY"
android:contentDescription="@string/benchmark_background" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_gravity="top|center"
android:layout_marginEnd="20dp"
android:background="@drawable/textview_bg_from_left"
android:padding="5dp"
android:text="@string/set_benchmark"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFF"
android:textSize="20sp" />
<TextView
android:id="@+id/textView19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textview"
android:layout_centerHorizontal="true"
android:paddingTop="10dp"
android:text="@string/benchmark"
android:textSize="19sp" />
<TextView
android:id="@+id/tv_benchmark_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView19"
android:layout_centerHorizontal="true"
android:text="." tools:ignore="HardcodedText"
android:textSize="19sp" />
</RelativeLayout>
</FrameLayout>
</RelativeLayout>
为此,您必须使用 scrollview 或 Proper linearLayout weightsum(复制并检查下面的代码)。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?actionBarSize"
android:orientation="vertical"
android:weightSum="2">
<RelativeLayout
android:id="@+id/rl5"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="5dp"
android:layout_weight="1.7"
android:orientation="vertical"
android:paddingHorizontal="10dp">
<LinearLayout
android:id="@+id/LL1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/LL2"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_benchmarks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#ffffff"
android:dividerHeight="1dp"
android:footerDividersEnabled="true"
android:headerDividersEnabled="true"
android:scrollbars="vertical" />
</LinearLayout>
<LinearLayout
android:id="@+id/LL2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/b_add_benchmark"
android:baselineAligned="false">
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_email"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="benHint"
android:inputType="text"
android:textSize="20sp" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_email2"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1">
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="hint size"
android:inputType="number"
android:textSize="20sp" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<android.support.v7.widget.AppCompatButton
android:id="@+id/b_add_benchmark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="@drawable/button_green"
android:padding="10dp"
android:text="Add"
android:textSize="20sp"
tools:gravity="center" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="?attr/actionBarSize"
android:layout_weight="0.25">
<View
android:id="@+id/View1"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:background="@android:color/darker_gray" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/View1">
<ImageView
android:id="@+id/iv7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.2"
android:contentDescription="Backgrounf"
android:scaleType="fitXY"
android:src="@drawable/logo" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_gravity="top|center"
android:layout_marginEnd="20dp"
android:background="@drawable/button_green"
android:padding="5dp"
android:text="Set"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFF"
android:textSize="20sp" />
<TextView
android:id="@+id/textView19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textview"
android:layout_centerHorizontal="true"
android:paddingTop="10dp"
android:text="benchmark"
android:textSize="19sp" />
<TextView
android:id="@+id/tv_benchmark_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView19"
android:layout_centerHorizontal="true"
android:text="."
android:textSize="19sp"
tools:ignore="HardcodedText" />
</RelativeLayout>
</FrameLayout>
</RelativeLayout>
</LinearLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.