[英]Trying to space buttons evenly for any screen in a LinearLayout, without stretching the images
我试过了这个问题的答案,唯一的区别是我使用的是垂直的LinearLayout而不是水平的:
因此,我发现我将围绕width / height属性进行交换,以便所有元素的宽度都将为wrap_content,高度为match_parent。 那应该是吗?
无论如何,我的体重选择仍然使我的按钮以可怕的方式伸展。
这是xml文件:
<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:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleX="0.8"
android:scaleY="0.8"
android:src="@drawable/logo" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:background="@drawable/button1_image"
android:onClick="button1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:background="@drawable/button2_image"
android:onClick="button2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:background="@drawable/button3_image"
android:onClick="button3" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:background="@drawable/button4_image"
android:onClick="button4" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:onClick="Button5"
android:text="Text"
android:textSize="26dp" />
</LinearLayout>
我感觉自己接近了-我在这里想念什么?
如果要防止某个项目被拉伸,请将其height和width属性设置为
android:layout_width="wrap_content"
android:layout_height="wrap_content"
因此,按钮将仅与给定的背景图像一样宽。
如果您的背景图片太大,那也可能会使事情歪斜。
我不太了解您的要求,但这对您有帮助吗?
<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:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleX="0.8"
android:scaleY="0.8"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/ic_launcher"
android:onClick="button1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/ic_launcher"
android:onClick="button2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/ic_launcher"
android:onClick="button3" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/ic_launcher"
android:onClick="button4" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:onClick="Button5"
android:text="Text"
android:textSize="26dp" />
</LinearLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.