【问题标题】:Carriage return in Button messes with the marginButton 中的回车与边距混淆
【发布时间】:2019-03-14 05:30:15
【问题描述】:

如果我在按钮中添加回车,顶部会出现额外的“边距”(由于缺乏更好的解释):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff992a2a">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="200dp">
        <Button
            android:layout_width="250dp"
            android:layout_height="100dp"
            android:text="Extend" />
        <Button
            android:layout_width="250dp"
            android:layout_height="100dp"
            android:text="Accept
0:23"/>
    </LinearLayout>
</RelativeLayout>

如果我不添加回车,它会完美对齐:

如何阻止出现额外的“边距”?我尝试设置android:lines="2" 和/或android:singleline="false",但似乎没有任何帮助。

【问题讨论】:

    标签: android android-layout button line-breaks


    【解决方案1】:

    你需要在你的LinearLayout中设置android:gravity="center"

    示例代码

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ff992a2a">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:gravity="center"
            android:orientation="horizontal">
    
            <Button
                android:layout_width="250dp"
                android:layout_height="100dp"
                android:text="Extend" />
    
            <Button
                android:layout_width="250dp"
                android:layout_height="100dp"
                android:text="Accept
    0:23" />
        </LinearLayout>
    </RelativeLayout>
    

    【讨论】:

      【解决方案2】:

      无需添加LinearLayout。您可以只使用RelativeLayout。 试试这个

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="#ff992a2a">
      
          <Button
              android:id="@+id/button_1"
              android:layout_width="250dp"
              android:layout_height="100dp"
              android:text="Extend" />
      
          <Button
              android:id="@+id/button_2"
              android:layout_width="250dp"
              android:layout_height="100dp"
              android:layout_toRightOf="@id/button_1"
              android:text="Accept
                  0:23" />
      
      </RelativeLayout>
      

      【讨论】:

      • mmm 其中一些按钮将被动态显示和隐藏,因此 toRightOf 将不起作用。
      • toRighOf 如果您的任何按钮的可见性为“消失”,则仍然可以使用
      • 我一直在使用 Xamarin 使用 local:MvxBind="Visibility ExtendButtonVisible, Converter=Visibility" 动态显示和隐藏按钮,并且从一开始就注意到,如果 toRightOf参考是隐藏的,所以我从那以后就没有使用过。也许我需要再做一次试验,也许我做错了什么。谢谢@你好世界
      猜你喜欢
      • 1970-01-01
      • 2013-05-19
      • 2017-09-29
      • 2014-07-09
      • 1970-01-01
      • 2020-11-23
      • 1970-01-01
      • 2015-03-23
      • 2019-10-24
      相关资源
      最近更新 更多