Android 尝试在线性布局中为任何屏幕均匀分配按钮空间,而不拉伸图像
我尝试了一下这个问题的答案,唯一的区别是我使用的是垂直线性布局,而不是水平布局: 因此,我想我应该交换width/height属性,以便所有元素的宽度都是wrap\u content,其中As height是match\u parent。应该是这样吗 无论如何,我的体重选择仍然导致我的按钮以可怕的方式伸展 以下是xml文件:Android 尝试在线性布局中为任何屏幕均匀分配按钮空间,而不拉伸图像,android,xml,layout,android-linearlayout,android-layout-weight,Android,Xml,Layout,Android Linearlayout,Android Layout Weight,我尝试了一下这个问题的答案,唯一的区别是我使用的是垂直线性布局,而不是水平布局: 因此,我想我应该交换width/height属性,以便所有元素的宽度都是wrap\u content,其中As height是match\u parent。应该是这样吗 无论如何,我的体重选择仍然导致我的按钮以可怕的方式伸展 以下是xml文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<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>
我觉得我离得很近了-我缺少了什么?如果要防止项目被拉伸,请将其“高度”和“宽度”属性设置为
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>
我试过了。但是有些东西仍然在挤压我的图像。你想要这个布局吗?