【问题标题】:ImageButton (right-aligned, fixed width) and EditText (left-aligned, remaining space width)ImageButton(右对齐,固定宽度)和 EditText(左对齐,剩余空间宽度)
【发布时间】:2013-04-29 17:27:39
【问题描述】:

我试图在同一行中放置一个ImageButton(右对齐,固定宽度)和一个EditText(左对齐,占用所有剩余空间),但我做不到。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
    android:layout_gravity="center_vertical"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:text="EditText"
    android:textSize="18sp" />
<ImageButton
    android:layout_gravity="center_vertical"
    android:layout_width="40dp"
    android:layout_height="40dp" />
</LinearLayout>

我无法使用属性"layout_weight",因为我想修复ImageButton 的宽度。如果我在EditText 中使用"layout_width=fill_parent"ImageButton 就会消失。

你能帮帮我吗?

谢谢!!!

【问题讨论】:

    标签: android android-layout right-align


    【解决方案1】:

    如果您不能使用layout_weight,请使用RelativeLayout。在RelativeLayout 中首先放入ImageButton 并将其对齐到RelativeLayout 的右侧。然后把EditTextlayout_width="fill_parent"放在ImageButton的左边

    试试这个

    <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
        <ImageButton 
            android:layout_width="40dp" 
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:id="@+id/id_image_button"/>
    
        <EditText android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_toLeftOf="@id/id_image_button"/>
    
    </RelativeLayout>
    

    【讨论】:

      【解决方案2】:
      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="50dp"
          android:orientation="horizontal" >
      <EditText
          android:layout_gravity="center_vertical"
          android:layout_width="0dp"
          android:layout_weight="1"
          android:layout_height="40dp"
          android:text="EditText"
          android:textSize="18sp" />
      <ImageButton
          android:layout_gravity="center_vertical"
          android:layout_width="40dp"
          android:layout_height="40dp" />
      </LinearLayout>
      

      只是我将 EditText 的 fill_parent 的 layout_width 更改为 0dp 并设置 layout_weight="1",所以它会被拉伸并占用剩余空间。

      Check this 用于 LinearLayout 权重概念。

      【讨论】:

      • 非常感谢您的回答。是的,android:layout_weight="1" /&gt; 完美运行。我误解了 layout_weight 属性的使用。我认为您必须为同一布局中的所有视图指定此属性,但现在我知道您只能在要影响的视图中使用它。再次感谢!!
      【解决方案3】:

      您可以尝试以下方法吗?:

      <EditText
      android:layout_gravity="center_vertical"
      android:layout_width="wrap_content"
      android:layout_height="40dp"
      android:text="EditText"
      android:textSize="18sp"
      android:layout_weight="1" />
      

      【讨论】:

      • 非常感谢您的回答。是的,android:layout_weight="1" /&gt; 完美运行。我误解了 layout_weight 属性的使用。我认为您必须为同一布局中的所有视图指定此属性,但现在我知道您只能在要影响的视图中使用它。再次感谢!!
      猜你喜欢
      • 2012-09-25
      • 2011-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多