【问题标题】:Simple Android Layout Resize简单的 Android 布局调整大小
【发布时间】:2015-03-17 03:17:25
【问题描述】:

我正在尝试调整旋转按钮的大小以按比例展开。现在,它们是固定长度,但我不确定让按钮调整大小的替代方法(设置重量似乎没有帮助)。我希望按钮基本上垂直和水平地填充屏幕的长度。如果您需要更多信息,请告诉我。

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

【问题讨论】:

    标签: android xml android-layout android-linearlayout android-button


    【解决方案1】:

    用这个替换你的资源。复制已编辑的更改

    请注意宽度是 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>
    

    【讨论】:

    • 如果您在答案中添加一些解释会很好。
    • 啊,明白了。对不起,我的错。
    • np 我试着为他画出更多的内容。一开始重量可能会让人难以理解
    • 不幸的是,这实际上并没有做任何事情:(
    • 确保将其放入资源文件中,我将进行必要的更改
    【解决方案2】:

    您需要为LinearLayout 指定方向。

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

    然后,为了使android:weight 工作,您需要为每个按钮使用android:layout_width="0px",将每个按钮的宽度指定为0px

    【讨论】:

      【解决方案3】:
          <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 属性调整按钮的大小以垂直和水平填充屏幕。

      【讨论】:

        【解决方案4】:

        您可以在布局权重的帮助下创建布局,使其在横向模式下自动填充屏幕,如下所示:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-04-30
          • 2012-09-05
          • 2012-02-20
          • 2011-11-20
          • 1970-01-01
          • 2011-10-09
          • 2014-07-14
          • 2011-08-31
          相关资源
          最近更新 更多