使 ImageView 至少占屏幕的 50%
Making the ImageView at least 50% of the screen
请看下面的代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:id="@+id/image_background"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:src="@drawable/mmm"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"/>
<LinearLayout
android:id="@+id/l22Parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_below="@+id/image_background"
android:orientation="horizontal">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="1"
android:id="@+id/step18button1"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:background="@drawable/button_border"
android:layout_below="@+id/step18textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="2"
android:id="@+id/step18button2"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignTop="@+id/step18button1"
android:layout_toRightOf="@+id/step18button1"
android:layout_toEndOf="@+id/step18button1"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="3"
android:id="@+id/step18button3"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button2"
android:layout_toRightOf="@+id/step18button2"
android:layout_toEndOf="@+id/step18button2"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="4"
android:id="@+id/step18button4"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button3"
android:layout_toRightOf="@+id/step18button3"
android:layout_toEndOf="@+id/step18button3"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="5"
android:id="@+id/step18button5"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button4"
android:layout_toRightOf="@+id/step18button4"
android:layout_toEndOf="@+id/step18button4"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="6"
android:id="@+id/step18button6"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button5"
android:layout_toRightOf="@+id/step18button5"
android:layout_toEndOf="@+id/step18button5"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="7"
android:id="@+id/step18button7"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button6"
android:layout_toRightOf="@+id/step18button6"
android:layout_toEndOf="@+id/step18button6"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="8"
android:id="@+id/step18button8"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button7"
android:layout_toRightOf="@+id/step18button7"
android:layout_toEndOf="@+id/step18button7"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="9"
android:id="@+id/step18button9"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button8"
android:layout_toRightOf="@+id/step18button8"
android:layout_toEndOf="@+id/step18button8"
android:background="@drawable/button_border"/>
</LinearLayout>
</RelativeLayout>
上面生成的UI如下
但是我希望图像至少占据屏幕的 50%。布局无关紧要,任何布局都可以,但在任何移动设备中,它应该占屏幕的 50% 左右(50% 是一个粗略值,可以是 60% 等)。如何达到我的要求?
我使用的图片可以在这里找到 - http://iespressolife.com/wp-content/uploads/2016/03/Malabar-InHouse-9423_1450_906px-1024x640.jpg
更新
用户"Arpit Patel"的回答是正确的,但不完整。以下是正确的版本。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/white">
<ImageView
android:id="@+id/image_background"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_weight="2"
android:adjustViewBounds="true"
android:background="@drawable/mmm" />
<LinearLayout
android:id="@+id/l22Parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/image_background"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/step18button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/step18textView"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:text="1"
android:textSize="13dp" />
<Button
android:id="@+id/step18button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/step18button1"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button1"
android:layout_toRightOf="@+id/step18button1"
android:layout_weight="1"
android:text="2"
android:textSize="13dp" />
<Button
android:id="@+id/step18button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button2"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button2"
android:layout_toRightOf="@+id/step18button2"
android:layout_weight="1"
android:text="3"
android:textSize="13dp" />
<Button
android:id="@+id/step18button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button3"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button3"
android:layout_toRightOf="@+id/step18button3"
android:layout_weight="1"
android:text="4"
android:textSize="13dp" />
<Button
android:id="@+id/step18button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button4"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button4"
android:layout_toRightOf="@+id/step18button4"
android:layout_weight="1"
android:text="5"
android:textSize="13dp" />
<Button
android:id="@+id/step18button6"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button5"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button5"
android:layout_toRightOf="@+id/step18button5"
android:layout_weight="1"
android:text="6"
android:textSize="13dp" />
<Button
android:id="@+id/step18button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button6"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button6"
android:layout_toRightOf="@+id/step18button6"
android:layout_weight="1"
android:text="7"
android:textSize="13dp" />
<Button
android:id="@+id/step18button8"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button7"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button7"
android:layout_toRightOf="@+id/step18button7"
android:layout_weight="1"
android:text="8"
android:textSize="13dp" />
<Button
android:id="@+id/step18button9"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button8"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button8"
android:layout_toRightOf="@+id/step18button8"
android:layout_weight="1"
android:text="9"
android:textSize="13dp" />
</LinearLayout>
</LinearLayout>
刚设置
android:weight="0.5"
在您的 ImageView 和 LinearLayout 标签内。您的图像将占据屏幕的一半,线性布局内的其他所有内容将占据屏幕的另一半。
试试这个 xml 代码。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2"
android:background="@android:color/white">
<ImageView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:layout_weight="1"
android:src="@drawable/mmm"
android:id="@+id/image_background"
android:adjustViewBounds="true"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1"
android:id="@+id/l22Parent"
android:layout_marginTop="10dp"
android:layout_below="@+id/image_background">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="1"
android:id="@+id/step18button1"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:background="@drawable/button_border"
android:layout_below="@+id/step18textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="2"
android:id="@+id/step18button2"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignTop="@+id/step18button1"
android:layout_toRightOf="@+id/step18button1"
android:layout_toEndOf="@+id/step18button1"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="3"
android:id="@+id/step18button3"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button2"
android:layout_toRightOf="@+id/step18button2"
android:layout_toEndOf="@+id/step18button2"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="4"
android:id="@+id/step18button4"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button3"
android:layout_toRightOf="@+id/step18button3"
android:layout_toEndOf="@+id/step18button3"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="5"
android:id="@+id/step18button5"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button4"
android:layout_toRightOf="@+id/step18button4"
android:layout_toEndOf="@+id/step18button4"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="6"
android:id="@+id/step18button6"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button5"
android:layout_toRightOf="@+id/step18button5"
android:layout_toEndOf="@+id/step18button5"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="7"
android:id="@+id/step18button7"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button6"
android:layout_toRightOf="@+id/step18button6"
android:layout_toEndOf="@+id/step18button6"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="8"
android:id="@+id/step18button8"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button7"
android:layout_toRightOf="@+id/step18button7"
android:layout_toEndOf="@+id/step18button7"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="9"
android:id="@+id/step18button9"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button8"
android:layout_toRightOf="@+id/step18button8"
android:layout_toEndOf="@+id/step18button8"
android:background="@drawable/button_border"/>
</LinearLayout>
</LinearLayout>
查看证据 50% 的屏幕被图像覆盖。
<ImageView
android:id="@+id/image_background"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="@drawable/a" />
<LinearLayout
android:id="@+id/l22Parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/image_background"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<Button
android:id="@+id/step18button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/step18textView"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:text="1"
android:textSize="13dp" />
<Button
android:id="@+id/step18button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/step18button1"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button1"
android:layout_toRightOf="@+id/step18button1"
android:layout_weight="1"
android:text="2"
android:textSize="13dp" />
<Button
android:id="@+id/step18button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button2"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button2"
android:layout_toRightOf="@+id/step18button2"
android:layout_weight="1"
android:text="3"
android:textSize="13dp" />
<Button
android:id="@+id/step18button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button3"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button3"
android:layout_toRightOf="@+id/step18button3"
android:layout_weight="1"
android:text="4"
android:textSize="13dp" />
<Button
android:id="@+id/step18button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button4"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button4"
android:layout_toRightOf="@+id/step18button4"
android:layout_weight="1"
android:text="5"
android:textSize="13dp" />
<Button
android:id="@+id/step18button6"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button5"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button5"
android:layout_toRightOf="@+id/step18button5"
android:layout_weight="1"
android:text="6"
android:textSize="13dp" />
<Button
android:id="@+id/step18button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button6"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button6"
android:layout_toRightOf="@+id/step18button6"
android:layout_weight="1"
android:text="7"
android:textSize="13dp" />
<Button
android:id="@+id/step18button8"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button7"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button7"
android:layout_toRightOf="@+id/step18button7"
android:layout_weight="1"
android:text="8"
android:textSize="13dp" />
<Button
android:id="@+id/step18button9"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button8"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button8"
android:layout_toRightOf="@+id/step18button8"
android:layout_weight="1"
android:text="9"
android:textSize="13dp" />
</LinearLayout>
将 LinearLayout
固定在底部,并告诉您的 ImageView
显示在其上方。
当然,您需要删除 layout_below
属性。
<RelativeLayout>
<ImageView
layout:above_layout="@+id/yourLinearLayout"/>
<LinearLayout
android:alignParentBottom="true"/>
</RelativeLayout>
您可以让 ImageView
在 space 中居中,对其进行裁剪等。此外,您可以为“LinearLayout”设置上边距以使其变大。
请看下面的代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:id="@+id/image_background"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:src="@drawable/mmm"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"/>
<LinearLayout
android:id="@+id/l22Parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_below="@+id/image_background"
android:orientation="horizontal">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="1"
android:id="@+id/step18button1"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:background="@drawable/button_border"
android:layout_below="@+id/step18textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="2"
android:id="@+id/step18button2"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignTop="@+id/step18button1"
android:layout_toRightOf="@+id/step18button1"
android:layout_toEndOf="@+id/step18button1"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="3"
android:id="@+id/step18button3"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button2"
android:layout_toRightOf="@+id/step18button2"
android:layout_toEndOf="@+id/step18button2"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="4"
android:id="@+id/step18button4"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button3"
android:layout_toRightOf="@+id/step18button3"
android:layout_toEndOf="@+id/step18button3"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="5"
android:id="@+id/step18button5"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button4"
android:layout_toRightOf="@+id/step18button4"
android:layout_toEndOf="@+id/step18button4"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="6"
android:id="@+id/step18button6"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button5"
android:layout_toRightOf="@+id/step18button5"
android:layout_toEndOf="@+id/step18button5"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="7"
android:id="@+id/step18button7"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button6"
android:layout_toRightOf="@+id/step18button6"
android:layout_toEndOf="@+id/step18button6"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="8"
android:id="@+id/step18button8"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button7"
android:layout_toRightOf="@+id/step18button7"
android:layout_toEndOf="@+id/step18button7"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="9"
android:id="@+id/step18button9"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button8"
android:layout_toRightOf="@+id/step18button8"
android:layout_toEndOf="@+id/step18button8"
android:background="@drawable/button_border"/>
</LinearLayout>
</RelativeLayout>
上面生成的UI如下
但是我希望图像至少占据屏幕的 50%。布局无关紧要,任何布局都可以,但在任何移动设备中,它应该占屏幕的 50% 左右(50% 是一个粗略值,可以是 60% 等)。如何达到我的要求?
我使用的图片可以在这里找到 - http://iespressolife.com/wp-content/uploads/2016/03/Malabar-InHouse-9423_1450_906px-1024x640.jpg
更新
用户"Arpit Patel"的回答是正确的,但不完整。以下是正确的版本。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/white">
<ImageView
android:id="@+id/image_background"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_weight="2"
android:adjustViewBounds="true"
android:background="@drawable/mmm" />
<LinearLayout
android:id="@+id/l22Parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/image_background"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/step18button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/step18textView"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:text="1"
android:textSize="13dp" />
<Button
android:id="@+id/step18button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/step18button1"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button1"
android:layout_toRightOf="@+id/step18button1"
android:layout_weight="1"
android:text="2"
android:textSize="13dp" />
<Button
android:id="@+id/step18button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button2"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button2"
android:layout_toRightOf="@+id/step18button2"
android:layout_weight="1"
android:text="3"
android:textSize="13dp" />
<Button
android:id="@+id/step18button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button3"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button3"
android:layout_toRightOf="@+id/step18button3"
android:layout_weight="1"
android:text="4"
android:textSize="13dp" />
<Button
android:id="@+id/step18button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button4"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button4"
android:layout_toRightOf="@+id/step18button4"
android:layout_weight="1"
android:text="5"
android:textSize="13dp" />
<Button
android:id="@+id/step18button6"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button5"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button5"
android:layout_toRightOf="@+id/step18button5"
android:layout_weight="1"
android:text="6"
android:textSize="13dp" />
<Button
android:id="@+id/step18button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button6"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button6"
android:layout_toRightOf="@+id/step18button6"
android:layout_weight="1"
android:text="7"
android:textSize="13dp" />
<Button
android:id="@+id/step18button8"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button7"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button7"
android:layout_toRightOf="@+id/step18button7"
android:layout_weight="1"
android:text="8"
android:textSize="13dp" />
<Button
android:id="@+id/step18button9"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button8"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button8"
android:layout_toRightOf="@+id/step18button8"
android:layout_weight="1"
android:text="9"
android:textSize="13dp" />
</LinearLayout>
</LinearLayout>
刚设置
android:weight="0.5"
在您的 ImageView 和 LinearLayout 标签内。您的图像将占据屏幕的一半,线性布局内的其他所有内容将占据屏幕的另一半。
试试这个 xml 代码。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2"
android:background="@android:color/white">
<ImageView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:layout_weight="1"
android:src="@drawable/mmm"
android:id="@+id/image_background"
android:adjustViewBounds="true"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1"
android:id="@+id/l22Parent"
android:layout_marginTop="10dp"
android:layout_below="@+id/image_background">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="1"
android:id="@+id/step18button1"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:background="@drawable/button_border"
android:layout_below="@+id/step18textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="2"
android:id="@+id/step18button2"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignTop="@+id/step18button1"
android:layout_toRightOf="@+id/step18button1"
android:layout_toEndOf="@+id/step18button1"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="3"
android:id="@+id/step18button3"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button2"
android:layout_toRightOf="@+id/step18button2"
android:layout_toEndOf="@+id/step18button2"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="4"
android:id="@+id/step18button4"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button3"
android:layout_toRightOf="@+id/step18button3"
android:layout_toEndOf="@+id/step18button3"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="5"
android:id="@+id/step18button5"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button4"
android:layout_toRightOf="@+id/step18button4"
android:layout_toEndOf="@+id/step18button4"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="6"
android:id="@+id/step18button6"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button5"
android:layout_toRightOf="@+id/step18button5"
android:layout_toEndOf="@+id/step18button5"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="7"
android:id="@+id/step18button7"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button6"
android:layout_toRightOf="@+id/step18button6"
android:layout_toEndOf="@+id/step18button6"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="8"
android:id="@+id/step18button8"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button7"
android:layout_toRightOf="@+id/step18button7"
android:layout_toEndOf="@+id/step18button7"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="9"
android:id="@+id/step18button9"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button8"
android:layout_toRightOf="@+id/step18button8"
android:layout_toEndOf="@+id/step18button8"
android:background="@drawable/button_border"/>
</LinearLayout>
</LinearLayout>
查看证据 50% 的屏幕被图像覆盖。
<ImageView
android:id="@+id/image_background"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="@drawable/a" />
<LinearLayout
android:id="@+id/l22Parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/image_background"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<Button
android:id="@+id/step18button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/step18textView"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:text="1"
android:textSize="13dp" />
<Button
android:id="@+id/step18button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/step18button1"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button1"
android:layout_toRightOf="@+id/step18button1"
android:layout_weight="1"
android:text="2"
android:textSize="13dp" />
<Button
android:id="@+id/step18button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button2"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button2"
android:layout_toRightOf="@+id/step18button2"
android:layout_weight="1"
android:text="3"
android:textSize="13dp" />
<Button
android:id="@+id/step18button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button3"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button3"
android:layout_toRightOf="@+id/step18button3"
android:layout_weight="1"
android:text="4"
android:textSize="13dp" />
<Button
android:id="@+id/step18button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button4"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button4"
android:layout_toRightOf="@+id/step18button4"
android:layout_weight="1"
android:text="5"
android:textSize="13dp" />
<Button
android:id="@+id/step18button6"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button5"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button5"
android:layout_toRightOf="@+id/step18button5"
android:layout_weight="1"
android:text="6"
android:textSize="13dp" />
<Button
android:id="@+id/step18button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button6"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button6"
android:layout_toRightOf="@+id/step18button6"
android:layout_weight="1"
android:text="7"
android:textSize="13dp" />
<Button
android:id="@+id/step18button8"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button7"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button7"
android:layout_toRightOf="@+id/step18button7"
android:layout_weight="1"
android:text="8"
android:textSize="13dp" />
<Button
android:id="@+id/step18button9"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button8"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button8"
android:layout_toRightOf="@+id/step18button8"
android:layout_weight="1"
android:text="9"
android:textSize="13dp" />
</LinearLayout>
将 LinearLayout
固定在底部,并告诉您的 ImageView
显示在其上方。
当然,您需要删除 layout_below
属性。
<RelativeLayout>
<ImageView
layout:above_layout="@+id/yourLinearLayout"/>
<LinearLayout
android:alignParentBottom="true"/>
</RelativeLayout>
您可以让 ImageView
在 space 中居中,对其进行裁剪等。此外,您可以为“LinearLayout”设置上边距以使其变大。