布局设计有两个按钮的问题

时间:2014-05-28 12:51:31

标签: javascript android android-layout layout

我想设计满足以下要求的布局:

1。最初有两个按钮。紧挨着另一个

enter image description here

2. 如果我需要增加button1的文本,则按钮2向右移动

enter image description here

3。这是问题所在,当你更多地增加文本时,button2缩小,我不想要,我希望button2不被缩小而button1应该是。

enter image description here

设计应如截图所示。

如何以呈现的方式设计布局?

7 个答案:

答案 0 :(得分:0)

试试这个:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
    android:id="@+id/button1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.1"
    android:text="Button" />

<Button
    android:id="@+id/button2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.1"
    android:text="Button" />

答案 1 :(得分:0)

实现这一目标的选择很少。

  1. 使用LinearLayout添加weightSum="3"并将layout_weight="integer"提供给子级。 这必须是比率格式 ex:2:1 ,在这种情况下还要确保layout_width="0dp"

  2. 使用Relative Layout,将 button2 添加到right_of 按钮1 ,然后将min_width="100dp"(调整大小)设置为的 BUTTON2

答案 2 :(得分:0)

在布局中使用以下内容

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="2"
    >

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />

</LinearLayout>

答案 3 :(得分:0)

试试这段代码:     

    <Button
        android:id="@+id/TextView07"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="button1 fgdfg Belgian Alesfghdfgh  dfgdfgddfgdfn drg" />

    <Button
        android:id="@+id/levadura_data_mejor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.001"
        android:text="Button2" />
</LinearLayout>

答案 4 :(得分:0)

试试这个,它正在运作:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="Button" />

    </LinearLayout>   

它也适用于所有设备。

答案 5 :(得分:0)

您需要将按钮包裹在另一个线性布局中才能使其正常工作。我就是这样做的:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:text="Button is really really long haha" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" >

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

答案 6 :(得分:0)

这是我能得到的最接近的。如果你能弄清楚如何使它在整体布局中左对齐而不是右对齐,它将符合你列出的要求:

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0" >

<Button
    android:id="@+id/test_button_0"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:text="Normal" >
</Button>

<Button
    android:id="@+id/test_button_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/test_button_0"
    android:lines="1"
    android:text="This could be very, very, very, long..." >
</Button>
</RelativeLayout>

通过使用相对布局而不是线性布局,此方法可以将右侧的按钮放在XML代码中。首先出现的孩子是布局管理员不会挤压的孩子,似乎。