簡體   English   中英

簡單的Android布局調整大小

[英]Simple Android Layout Resize

我正在嘗試調整旋轉按鈕的大小以按比例擴大。 現在,它們是固定長度,但是我不確定讓按鈕調整大小的另一種方法(設置權重似乎無濟於事)。 我希望按鈕在本質上可以垂直和水平填充屏幕的長度。 如果您需要更多信息,請告訴我。

 <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_gravity="center_vertical" >

            <Button
                android:id="@+id/mainCommentBtn"
                android:layout_width="119dp"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:clickable="true"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:layout_weight="1"
                android:text="@string/commentBtn" />

            <Button
                android:id="@+id/mainProfileBtn"
                android:layout_width="119dp"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:layout_weight="1"
                android:layout_centerInParent="true"
                android:text="@string/profileBtn" />

            <Button
                android:id="@+id/mainDetailBtn"
                android:layout_width="119dp"
                android:clickable="true"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="@string/shareBtn" />
        </LinearLayout>

替換您的資源。 復制修改的更改

注意寬度是0dp,因為它們正在使用重量。 使用權重:1.要使用權重,您需要在父級中設置weightSum。 2.確保根據體重的使用方式將寬度或高度設置為0。 (例如,由於我們要使用水平方向,因此我們想使用“ 0dp”寬度。這允許權重控制對象的寬度。3.最后,確保所有對象的權重加起來等於weightSum。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:weightSum="3" >

<Button
    android:id="@+id/mainCommentBtn"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_weight="1"
    android:clickable="true"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:text="@string/commentBtn" />

<Button
    android:id="@+id/mainProfileBtn"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_weight="1"
    android:clickable="true"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:text="@string/profileBtn" />

<Button
    android:id="@+id/mainDetailBtn"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_weight="1"
    android:clickable="true"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:text="@string/shareBtn" />

</LinearLayout>

您需要為LinearLayout指定方向。

我猜你想使用android:orientation="horizontal"

然后,為了使android:weight工作,您需要通過對每個按鈕使用android:layout_width="0px"將每個按鈕的寬度指定為0px

    <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Button" />

這些xml屬性可調整按鈕的大小,以垂直和水平填充屏幕。

您可以在布局權重的幫助下創建布局,以使其在橫向模式下也自動填充屏幕,如下所示:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="3" >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 1"
            android:layout_weight="1" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 2"
            android:layout_weight="1" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 3"
            android:layout_weight="1" />
    </LinearLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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