簡體   English   中英

嘗試為LinearLayout中的任何屏幕均勻地間隔按鈕,而不拉伸圖像

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM