【问题标题】:Java android custom dialog button does not fitJava android自定义对话框按钮不适合
【发布时间】:2017-03-03 12:50:40
【问题描述】:

我创建了一个自定义对话框,但并非所有按钮都适合。我不知道为什么会这样,在android studio中预览看起来不错,但在手机中却不好看。

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >


    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Wybierz grupę:"
        android:textColor="#000000"
       />


    <Spinner
        android:id="@+id/fd_valSp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="20dp" />

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


        <Button android:id="@+id/cancel_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/cancel" />


        <Button android:id="@+id/add_group_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/add_new_group"
             />

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

    </LinearLayout>

</RelativeLayout>

enter image description here

【问题讨论】:

    标签: java android android-layout android-xml


    【解决方案1】:

    在 XML 下使用

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
    
        <TextView
            android:layout_alignParentTop="true"
            android:id="@+id/text"
            android:gravity="center"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Wybierz grupę:"
            android:textColor="#000000" />
    
    
        <Spinner
    
            android:id="@+id/fd_valSp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/text"
            android:layout_marginTop="20dp" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/fd_valSp"
            android:weightSum="10"
            android:orientation="horizontal">
    
    
            <Button
                android:id="@+id/cancel_button"
                android:layout_width="0dp"
                android:layout_weight="2.5"
                android:layout_height="wrap_content"
                android:text="@string/cancel" />
    
    
            <Button
                android:id="@+id/add_group_button"
                android:layout_width="0dp"
                android:layout_weight="5"
                android:layout_height="wrap_content"
                android:text="@string/add_new_group" />
    
            <Button
                android:id="@+id/add"
                android:layout_width="0dp"
                android:layout_weight="2.5"
                android:layout_height="wrap_content"
                android:text="@string/add" />
    
        </LinearLayout>
    
    </RelativeLayout>
    

    【讨论】:

      【解决方案2】:

      您可以将 layout_weight 与线性布局一起使用,例如 0.3、0.4、0.3,这将适合其中的按钮。

      如果字符串很长,也可以垂直显示 3 个按钮。

      【讨论】:

        【解决方案3】:

        尝试在线性布局中使用 weight 属性,它会为你工作。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-06-15
          • 1970-01-01
          • 2016-05-16
          • 2021-02-06
          • 2021-07-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多