【问题标题】:Issue with Layout Design having the two buttons具有两个按钮的布局设计问题
【发布时间】:2014-07-17 16:58:32
【问题描述】:

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

1.最初有两个按钮。一个接一个

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

3. 问题是,当你增加更多的文本时,button2 会缩小,我不希望这样,我希望 button2 不被缩小,而 button1 应该是。

设计应如屏幕截图所示。

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

【问题讨论】:

  • 我不能使用权重,因为按钮的扩展是基于按钮内的文本
  • 您可以通过将 android:singleline 设置为 true 来避免这种情况,请参阅我的回答

标签: javascript android android-layout layout


【解决方案1】:

在你的布局中使用以下一个

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

【讨论】:

    【解决方案2】:

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

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

    2. 使用Relative Layout,把button2放到right_ofbutton1,把min_width="100dp"(调整大小)放到button2强>

    【讨论】:

    • 请检查添加到问题中的注释。谢谢
    • @user3607798 使用我的2 选项,这将满足以上所有条件。
    • 对不起..按钮2还在缩小
    【解决方案3】:

    试试这个:

    <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 个屏幕截图..请检查为按钮提供小文本
    • 不,它不会失败您的屏幕截图 1。正确检查。给你的按钮宽度 0 dp
    • 按钮的宽度不能是父级的一半..宽度必须取决于按钮的文本..检查屏幕截图1清楚
    【解决方案4】:

    试试这个代码:

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

    【讨论】:

    • 它对我有用。满足您的所有要求。
    • 尝试使用小文本作为按钮1...它不会满足 ScreenShot1
    • 对不起老板!但它也适用于我的小文本。我认为你做错了什么。
    【解决方案5】:

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

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

    【讨论】:

    • 一旦通过减少按钮1的文本来检查答案
    • 我知道它会缩小,因为它被设置为包装内容只需更改它以匹配您需要自己做的事情的父母我们只是在这里以我们自己的方式提供帮助,我们希望得到帮助无论如何我也会编辑我的答案
    【解决方案6】:

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

    <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 代码中的第一个位置。最先出现的孩子似乎是布局管理器不会挤压的孩子。

    【讨论】:

      【解决方案7】:

      试试这个,它的工作原理:

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

      它也适用于所有设备。

      【讨论】:

      • 请检查添加到问题中的注释
      猜你喜欢
      • 2013-01-31
      • 2019-02-16
      • 1970-01-01
      • 1970-01-01
      • 2017-07-22
      • 2014-11-04
      • 1970-01-01
      • 1970-01-01
      • 2013-12-08
      相关资源
      最近更新 更多