简体   繁体   English

简单的Android布局调整大小

[英]Simple Android Layout Resize

I'm attempting to resize buttons on rotate to expand proportionately. 我正在尝试调整旋转按钮的大小以按比例扩大。 Right now, they are a fix length, but I'm unsure of an alternative method to get the buttons to resize (setting the weight doesn't seem to help). 现在,它们是固定长度,但是我不确定让按钮调整大小的另一种方法(设置权重似乎无济于事)。 I want the buttons to essentially fill the length of the screen both vertically and horizontally. 我希望按钮在本质上可以垂直和水平填充屏幕的长度。 Please let me know if you need more information. 如果您需要更多信息,请告诉我。

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

Replace your resource with this. 替换您的资源。 Copy the edited changes 复制修改的更改

Notice how there width is 0dp because they are using weight. 注意宽度是0dp,因为它们正在使用重量。 Using Weight: 1. In order to use weight you need to set the weightSum in the parent. 使用权重:1.要使用权重,您需要在父级中设置weightSum。 2. Make sure the width or height is set to 0 according to how you are using weight. 2.确保根据体重的使用方式将宽度或高度设置为0。 (for instance since we are going with horizontal orientation we want to use "0dp" width. This allows weight to control the width of the object. 3. Lastly make sure the weight of all the objects add up to the weightSum. (例如,由于我们要使用水平方向,因此我们想使用“ 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>

You need to specify an orientation for the LinearLayout . 您需要为LinearLayout指定方向。

I guess you want to use android:orientation="horizontal" . 我猜你想使用android:orientation="horizontal"

Then, in order to make android:weight work, you'd need to specify the width of every button as 0px by using android:layout_width="0px" for every button. 然后,为了使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" />

These xml properties size the button to fill the screen both vertically and horizontally. 这些xml属性可调整按钮的大小,以垂直和水平填充屏幕。

You can create the layout with the help of layout weight so that it will automatically fill the screen in landscape mode also as below: 您可以在布局权重的帮助下创建布局,以使其在横向模式下也自动填充屏幕,如下所示:

<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