简体   繁体   English

尝试为LinearLayout中的任何屏幕均匀地间隔按钮,而不拉伸图像

[英]Trying to space buttons evenly for any screen in a LinearLayout, without stretching the images

I gave the answer in this question a try, the only difference being that I'm using a vertical LinearLayout instead of horizontal: 我试过了这个问题的答案,唯一的区别是我使用的是垂直的LinearLayout而不是水平的:

Children in Linear Layout with equal padding 线性布局中的子项具有相同的填充

As such, I figured that I would swap around the width/height attributes so that width for all elements would be wrap_content, where as height would be match_parent. 因此,我发现我将围绕width / height属性进行交换,以便所有元素的宽度都将为wrap_content,高度为match_parent。 Is that how it should be? 那应该是吗?

Anyway, my weight selection is still causing my buttons to stretch in horrible ways. 无论如何,我的体重选择仍然使我的按钮以可怕的方式伸展。

Here is the xml file: 这是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>

I feel like I'm close - what am I missing here? 我感觉自己接近了-我在这里想念什么?

If you want to prevent an item from being stretched, set it's height and width attributes to 如果要防止某个项目被拉伸,请将其height和width属性设置为

android:layout_width="wrap_content"
android:layout_height="wrap_content"

So the buttons will only be as wide as the background image given. 因此,按钮将仅与给定的背景图像一样宽。

If your background image is too large, that can be skewing things as well. 如果您的背景图片太大,那也可能会使事情歪斜。

i don't know excetly what you require, but is this helpful to you? 我不太了解您的要求,但这对您有帮助吗?

<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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM