[英]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.