[英]layout change with screen size
我已經創建了一個xml文件,但是問題是,當手機的屏幕尺寸改變時,設計也會有所變化。我進行了搜索,但仍然感到困惑。不取決於它的屏幕尺寸。以下是我的xml文件-
player.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="6"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="#fff">
<Button
android:layout_width="70dp"
android:layout_height="45dp"
android:background="@drawable/backbtn"
android:textSize="19dp"
android:id="@+id/back"
/>
<TextView
android:layout_width="335dp"
android:layout_height="45dp"
android:text="Welcome"
android:textSize="30dp"
android:background="#fff"
android:textColor="#000"
android:layout_marginRight="40dp"
android:paddingRight="40dp"
android:layout_alignParentTop="true"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/textView3" />
<TextView
android:layout_width="20dp"
android:layout_height="30dp"
android:layout_weight="1"
android:layout_marginStart="10dp"
android:id="@+id/id"
android:layout_marginLeft="100dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
>
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="@+id/plname"
android:text="ABC XYZ"
android:layout_gravity="center"
android:layout_weight="1"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#fff"
android:paddingLeft="5dp"
android:background="#7ee77c"
android:gravity="left|center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="@+id/plid"
android:layout_toRightOf="@+id/plname"
android:layout_weight="1"
android:text="Player ID:"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#fff"
android:paddingLeft="10dp"
android:layout_gravity="center"
android:gravity="right|center"
android:background="#7ee77c"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_weight="1"
android:id="@+id/plidtxt"
android:text="1000"
android:textColor="#fff"
android:background="#7ee77c"
android:textSize="20dp"
android:paddingTop="2dp"
android:textStyle="bold"
android:layout_gravity="center"
android:paddingLeft="10dp"
android:gravity="left|center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
>
<TextView
android:layout_width="170dp"
android:layout_height="29dp"
android:id="@+id/day"
android:layout_below="@id/line2"
android:text="Wednesday"
android:textStyle="bold"
android:textSize="20dp"
android:gravity="right|center"
android:textColor="#000"
android:paddingRight="10dp"
android:background="@android:color/transparent"
/>
<TextView
android:layout_width="115dp"
android:layout_height="29dp"
android:id="@+id/date"
android:layout_below="@+id/line2"
android:layout_toRightOf="@id/day"
android:text="05/04/2017"
android:textStyle="bold"
android:textSize="20dp"
android:gravity="left|center"
android:textColor="#000"
android:paddingLeft="3dp"
android:background="@android:color/transparent"
/>
<TextView
android:layout_width="95dp"
android:layout_height="29dp"
android:id="@+id/time"
android:layout_below="@id/line3"
android:layout_toRightOf="@+id/date"
android:text="12.00"
android:textColor="#000"
android:textSize="20dp"
android:paddingBottom="4dp"
android:background="@android:color/transparent"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="10dp"
>
<SearchView
android:layout_width="290dp"
android:layout_height="45dp"
android:id="@+id/search"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:queryHint="Search"
android:layout_gravity="center_horizontal"
android:layout_below="@+id/day"
android:layout_centerHorizontal="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
>
<TextView
android:layout_width="40dp"
android:layout_height="match_parent"
android:text="Sr"
android:id="@+id/sr"
android:paddingTop="5dp"
android:textSize="20dp"
android:layout_marginTop="7dp"
android:textStyle="bold"
android:textColor="#fff"
android:background="#b27ce7"
android:gravity="center"
/>
<TextView
android:layout_width="145dp"
android:layout_height="match_parent"
android:text="Player Name"
android:layout_toRightOf="@+id/sr"
android:id="@+id/player"
android:paddingTop="5dp"
android:layout_weight="1"
android:textSize="20dp"
android:layout_marginTop="7dp"
android:textStyle="bold"
android:textColor="#fff"
android:background="#b27ce7"
android:gravity="center"
/>
<TextView
android:layout_width="28dp"
android:layout_height="match_parent"
android:id="@+id/a"
android:layout_weight="0.12"
android:layout_marginTop="7dp"
android:layout_toRightOf="@+id/player"
android:background="#b27ce7"
/>
<TextView
android:layout_width="40dp"
android:layout_height="43dp"
android:layout_toRightOf="@+id/a"
android:id="@+id/rank"
android:text="Rank"
android:layout_marginTop="7dp"
android:textSize="20dp"
android:textStyle="bold"
android:paddingBottom="1dp"
android:textColor="#fff"
android:gravity="center"
android:background="#b27ce7"
android:layout_weight="1" />
<TextView
android:layout_width="27dp"
android:layout_height="match_parent"
android:id="@+id/b"
android:layout_weight="0.12"
android:layout_marginTop="7dp"
android:layout_marginBottom="1dp"
android:layout_toRightOf="@+id/rank"
android:background="#b27ce7"
/>
<TextView
android:layout_width="40dp"
android:layout_height="match_parent"
android:background="#b27ce7"
android:layout_marginTop="7dp"
android:text="Team"
android:paddingTop="5dp"
android:layout_toRightOf="@+id/b"
android:textStyle="bold"
android:textColor="#fff"
android:textSize="20dp"
android:gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
>
<ListView
android:layout_width="330dp"
android:layout_height="250dp"
android:id="@+id/pllist"
android:background="#ffff"
android:layout_weight="1"
android:scrollbars="vertical"
android:divider="@null"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:dividerHeight="0dp"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:layout_below="@+id/listline1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<Button
android:layout_width="match_parent"
android:layout_height="42dp"
android:id="@+id/update"
android:background="#4e57a9"
android:text="Update"
android:textColor="#000"
android:textSize="25dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:textAllCaps="false"
android:layout_above="@+id/delete"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<Button
android:layout_width="match_parent"
android:layout_height="42dp"
android:layout_alignParentBottom="true"
android:id="@+id/delete"
android:layout_centerHorizontal="true"
android:background="#4e57a9"
android:text="Delete"
android:textColor="#000"
android:layout_marginBottom="15dp"
android:textSize="25dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textAllCaps="false"
/>
</LinearLayout>
</LinearLayout>
我認為您必須定義主要線性布局的高度和寬度。 像這樣
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="100dip"
android:layout_height="200dip"
android:weightSum="6"
>
幸運的是,這是一件容易的事,因為android通常會為您處理它。 您只需要為要支持的每個不同的屏幕尺寸創建一個不同的布局文件。
重要的部分是使用相同的名稱( example.xml
),但將它們放置在帶有某些標簽的不同文件夾中,這些標簽在何時應使用android時標識為android。 例如res/values/example.xml
與res/values-sw600dp-land/example.xml
,其中sw600dp和land是限定最小寬度和橫向的限定符。 你可以在這里讀更多關於它的內容。 https://developer.android.com/training/multiscreen/screensizes.html
不分配固定的高度和寬度給widget.use layout_weight屬性並設置所有小部件。
否則使用
res/layout/my_layout.xml // layout for normal screen size ("default")
res/layout-large/my_layout.xml // layout for large screen size
res/layout-xlarge/my_layout.xml // layout for extra-large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra-large in landscape orientation
res/drawable-mdpi/graphic.png // bitmap for medium-density
res/drawable-hdpi/graphic.png // bitmap for high-density
res/drawable-xhdpi/graphic.png // bitmap for extra-high-density
res/drawable-xxhdpi/graphic.png // bitmap for extra-extra-high-density
res/mipmap-mdpi/my_icon.png // launcher icon for medium-density
res/mipmap-hdpi/my_icon.png // launcher icon for high-density
res/mipmap-xhdpi/my_icon.png // launcher icon for extra-high-density
res/mipmap-xxhdpi/my_icon.png // launcher icon for extra-extra-high-density
res/mipmap-xxxhdpi/my_icon.png // launcher icon for extra-extra-extra-high-density
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.