无论屏幕尺寸如何,将两个按钮设置为相同宽度?

时间:2021-10-25 14:05:06

Ok, i have two buttons in linear layout:

好的,我有两个线性布局按钮:

<LinearLayout android:id="@+id/linearLayout1" 
              android:layout_height="wrap_content" 
              android:layout_width="fill_parent">
        <Button android:id="@+id/aktiviraj_paket" 
                android:text="Aktiviraj" 
                android:layout_height="40sp" 
                android:layout_width="160sp" 
                android:background="@drawable/my_border3" 
                android:onClick="myClickHandle"></Button>
        <Button android:id="@+id/deaktiviraj_paket" 
                android:text="Deaktiviraj" 
                android:layout_height="40sp" 
                android:layout_width="fill_parent" 
                android:background="@drawable/my_border3"
                android:onClick="myClickHandle">
        </Button>
</LinearLayout>

So the thing is, if I use fill parent on both buttons, they are one on each other, so i have made first button 160sp width, and second is fill_parent. If this is shown on 4 inch screen or smaller, buttons are the same size, but if i try this on tablet (10 inch) first button stays 160sp wide, and second is stretched till the end of screen (because fill_parent). Can i make this, so both buttons could be even size in no matter what size is the screen ??

所以问题是,如果我在两个按钮上使用填充父项,它们是一个在一起,所以我已经制作了第一个按钮160sp宽度,第二个是fill_parent。如果这个显示在4英寸或更小的屏幕上,按钮大小相同,但如果我在平板电脑上尝试这个(10英寸),第一个按钮保持160sp宽,第二个按钮延伸到屏幕结束(因为fill_parent)。我可以做到这一点,所以无论屏幕尺寸大小,两个按钮的尺寸都可以均匀吗?

7 个解决方案

#1


93  

Use android:layout_weight="1" on both Buttons. Set android:layout_width="0dp" on both. Since both buttons now have equal weighting, they will now each have half the parent's width.

在两个按钮上使用android:layout_weight =“1”。在两者上设置android:layout_width =“0dp”。由于两个按钮现在具有相同的权重,现在它们每个都具有父亲宽度的一半。

You can find out more here: http://developer.android.com/guide/topics/ui/layout/linear.html

您可以在此处找到更多信息:http://developer.android.com/guide/topics/ui/layout/linear.html

#2


15  

If what you're looking to do is to make all the buttons the width of the widest button, setting weights isn't going to do that. Rather, you can put all the buttons in a TableLayout:

如果您要做的是将所有按钮设置为最宽按钮的宽度,则设置权重不会这样做。相反,您可以将所有按钮放在TableLayout中:

   <TableLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/short_text" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/enter_manually" />
        </TableRow>
    </TableLayout>

This layout will show 2 buttons, one on top of the other, the same width.

此布局将显示2个按钮,一个在另一个的顶部,相同的宽度。

#3


4  

Display display=getWindowManager().getDefaultDisplay();
    int width=display.getWidth();
    btn1.setWidth(width/2);
    btn2.seTwidth(width/2);

Set anything in xml file then first find width of device then set width half to both button Now On every device they will look exactly same

在xml文件中设置任何内容然后首先找到设备的宽度然后将宽度设置为两个按钮现在在每个设备上它们看起来完全相同

#4


3  

set to each button:

设置为每个按钮:

android:layout_weight="0.5"
android:layout_width="0dp"

#5


1  

android:layout_weight="0.5"
android:layout_width="0dp

it's working

它正在工作

#6


0  

Set android:layout_weight="1" in the containing layout The linear layout should have android:orientation set as horizontal. And then the inside buttons should have the following:

在包含的布局中设置android:layout_weight =“1”线性布局应该将android:orientation设置为水平。然后内部按钮应具有以下内容:

android:layout_width="0dp"

机器人:layout_width = “0dp”

android:layout_weight="0.5"

机器人:layout_weight = “0.5”

#7


0  

<LinearLayout
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:text="One" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:text="Two" />
</LinearLayout>

These is the example for equal size buttons for side by side can be done from above code

这些是相同尺寸按钮的示例,可以从上面的代码中完成

android:layout_weight 

is used to assign space for buttons or whatever of equal amount for every child of LinearLayout.

用于为LinearLayout的每个子节点分配按钮或等量的空间。

Note: It works only on linear layout.

注意:它仅适用于线性布局。

#1


93  

Use android:layout_weight="1" on both Buttons. Set android:layout_width="0dp" on both. Since both buttons now have equal weighting, they will now each have half the parent's width.

在两个按钮上使用android:layout_weight =“1”。在两者上设置android:layout_width =“0dp”。由于两个按钮现在具有相同的权重,现在它们每个都具有父亲宽度的一半。

You can find out more here: http://developer.android.com/guide/topics/ui/layout/linear.html

您可以在此处找到更多信息:http://developer.android.com/guide/topics/ui/layout/linear.html

#2


15  

If what you're looking to do is to make all the buttons the width of the widest button, setting weights isn't going to do that. Rather, you can put all the buttons in a TableLayout:

如果您要做的是将所有按钮设置为最宽按钮的宽度,则设置权重不会这样做。相反,您可以将所有按钮放在TableLayout中:

   <TableLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/short_text" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/enter_manually" />
        </TableRow>
    </TableLayout>

This layout will show 2 buttons, one on top of the other, the same width.

此布局将显示2个按钮,一个在另一个的顶部,相同的宽度。

#3


4  

Display display=getWindowManager().getDefaultDisplay();
    int width=display.getWidth();
    btn1.setWidth(width/2);
    btn2.seTwidth(width/2);

Set anything in xml file then first find width of device then set width half to both button Now On every device they will look exactly same

在xml文件中设置任何内容然后首先找到设备的宽度然后将宽度设置为两个按钮现在在每个设备上它们看起来完全相同

#4


3  

set to each button:

设置为每个按钮:

android:layout_weight="0.5"
android:layout_width="0dp"

#5


1  

android:layout_weight="0.5"
android:layout_width="0dp

it's working

它正在工作

#6


0  

Set android:layout_weight="1" in the containing layout The linear layout should have android:orientation set as horizontal. And then the inside buttons should have the following:

在包含的布局中设置android:layout_weight =“1”线性布局应该将android:orientation设置为水平。然后内部按钮应具有以下内容:

android:layout_width="0dp"

机器人:layout_width = “0dp”

android:layout_weight="0.5"

机器人:layout_weight = “0.5”

#7


0  

<LinearLayout
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:text="One" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:text="Two" />
</LinearLayout>

These is the example for equal size buttons for side by side can be done from above code

这些是相同尺寸按钮的示例,可以从上面的代码中完成

android:layout_weight 

is used to assign space for buttons or whatever of equal amount for every child of LinearLayout.

用于为LinearLayout的每个子节点分配按钮或等量的空间。

Note: It works only on linear layout.

注意:它仅适用于线性布局。