开发者

Android: EditText next to ImageView - EditText as wide as possible

I'd like to have an EditText and a开发者_运维百科n ImageView next to each other; the ImageView has a fixed width, the EditText should take the rest.

I try to do it via

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal" android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical" android:layout_width="fill_parent"
                android:layout_height="wrap_content">
                <EditText android:id="@+id/edit_select_stop" android:layout_width="fill_parent" android:layout_height="wrap_content" />
            </LinearLayout>
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical" android:layout_width="fill_parent"
                android:layout_height="wrap_content">
                <ImageView android:layout_gravity="center_vertical"
                    android:layout_width="39dp" android:minWidth="39dp"
                    android:layout_height="wrap_content" android:src="@drawable/icon_time"
                    android:id="@+id/image_select_time"></ImageView>
            </LinearLayout>
        </LinearLayout>

But now, the EditText takes the whole width and covers the Image.

How can I achieve that EditText "ends" before the ImageView?


You can also use RelativeLayout to accomplish it. Simply assign the properties

 android:layout_toLeftOf="@+id/id_of_your_imageview" 
 android:layout_width="fill_parent"

to the EditText and

android:layout_alignParentRight="true"

to your ImageView while keeping the fixed value of the width.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget32"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<EditText
android:id="@+id/edit_select_stop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
>
</EditText>
<ImageView
android:id="@+id/image_select_time"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_weight="1"
>
</ImageView>
</LinearLayout>


Well I dont recommend to go for the relative layout..

for the code (2nd one) seems good but I'd like to add that you should set the gravity android:gravity="center_vertical"

This aligns the components and makes it look good for sure

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜