Set Textview + Edittext + Button

后端 未结 4 1702
独厮守ぢ
独厮守ぢ 2021-01-23 07:50

I want to put in the same row a TextView, and Edittext and a button but I am having the problem that the button is not aligned properly to left and in small screens edittext fil

相关标签:
4条回答
  • 2021-01-23 08:07

    I'm going to assume you mean the button is not properly aligned to the right.

    It's because your RelativeLayout's android:width="wrap_content", but it should be android:width="match_parent".

    Also, you'd be better off setting your EditText's android:width="0dp" and adding android:weight="1" so that it expands/contracts between screen sizes.

    0 讨论(0)
  • 2021-01-23 08:13

    Apply a weight to your EditText so it will take up as much room as it can while letting the other two elements do the normal wrap_content. To do this, remove the relative layout container and then change the EditText width to "0dp" and give it a layout_weight of "1" as follows:

    <LinearLayout
        android:id="@+id/layout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <TextView
            android:id="@+id/address_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/address_textview"
            android:textStyle="bold" />
    
        <EditText
            android:id="@+id/address_edittext"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:hint="@string/address_textview_hint"
            android:imeActionLabel="@string/search_button"
            android:imeOptions="actionDone"
            android:inputType="textCapWords"
            android:nextFocusDown="@id/address_edittext"
            android:nextFocusUp="@id/address_edittext"
            android:singleLine="true" />
    
        <Button
            android:id="@+id/go_button"
            android:layout_width="50dp"
            android:layout_height="35dp"
            android:text="Go" />
    
    </LinearLayout>
    
    0 讨论(0)
  • 2021-01-23 08:20

    Layout weigth is ideal for designing layouts that adjust to screen size. However, make sure to set layout_width to 0dp, or it won't work properly.

    Use like this:

        android:layout_width="0dp"
        android:layout_weight="1"
    
    0 讨论(0)
  • 2021-01-23 08:23

    First, many people will tell you that hint is Android's solution for not needing the label. I don't care if you use the label or not but it does save you space, especially on smaller screens. That was just an FYI.

    Now, your RelativeLayout that only has a Button appears to be useless...I would remove that. You can use layout_weight so that each View takes up the appropriate amount of space. Make sure to make the layout_width="0dp". So it may look something like

    <LinearLayout
        android:id="@+id/layout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    
        <TextView
            android:id="@+id/address_textview"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:gravity="center"
            android:text="@string/address_textview"
            android:textStyle="bold" />
    
        <EditText
            android:id="@+id/address_edittext"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:hint="@string/address_textview_hint"
            android:imeActionLabel="@string/search_button"
            android:imeOptions="actionDone"
            android:inputType="textCapWords"
            android:nextFocusDown="@id/address_edittext"
            android:nextFocusUp="@id/address_edittext"
            android:singleLine="true" />
    
        <Button
            android:id="@+id/go_button"
            android:layout_width="0dp"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:text="Go" />
    
    </LinearLayout>
    

    Here I used 2,3,1 for the weights of your TextView, EditText, and Button respectively. You may need to change those to get exactly what you want but that should give you a start.

    0 讨论(0)
提交回复
热议问题