繁体   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