开发者

How to put an EditText and Button next to each other?

I want to try to get an EditText and Button next to each other. I currently have where the button is on the right and the edit text is left aligned but the button then shows that it is on top of the EditText. I want the Button to start where the EditText ends.

This is what I have right now:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#004D79">
        <RelativeLayout android:id="@+id/relativeLayout1" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content">
        <EditText 
            android:layout_alignParentLeft="true" 
            android:text="EditText"
            android:layout_marginTop="10dp" 
            android:id="@+id/editText" 
            android:layout_height="wrap_content" 
            android:layout_width="fill_parent">
        </EditText>
        <Button android:text="Skip"
         android:id="@+id/skipButton" 
         android:textSize="18sp"
         android:layout_marginTop="10dp" 
         android开发者_如何学运维:layout_alignParentRight="true" 
         android:layout_height="wrap_content" 
         android:layout_width="80dp">
         </Button>
    </RelativeLayout> 
</LinearLayout>

I tried to set it up with making the EditText width with an exact dp amount but then when the screen flips horizontal there is a big gap between the EditText and Button.


If you want to continue to use a relative layout and have the edit text to go up until the button you can add this to the edittext xml

android:layout_toLeftOf="@+id/skipButton"


Instead of using the RelativeLayout, just change your LinearLayout to have android:orientation="horizontal". Then all the views inside it will be put side to side. Remove the alignParent things (those are for relative layouts), and set the editText (which I believe you want to stretch and fill the screen) to have layout_weight="1".

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#004D79"
    >
        <EditText 
            android:layout_alignParentLeft="true" 
            android:text="EditText"
            android:layout_weight="1" 
            android:id="@+id/editText" 
            android:layout_height="wrap_content" 
            android:layout_width="fill_parent">
        </EditText>
        <Button android:text="Skip"
         android:id="@+id/skipButton" 
         android:textSize="18dp"
         android:layout_marginTop="10dp" 
         android:layout_height="wrap_content" 
         android:layout_width="80dp">
         </Button>
</LinearLayout>


    <LinearLayout
        android:id="@+id/measure"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/product_description"
        android:layout_margin="10dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="20dp"
        android:background="#ff669900"
        android:orientation="horizontal"
        android:padding="1px"
        android:textColor="#fff"
        android:weightSum="2" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:ellipsize="marquee"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:freezesText="true"
            android:gravity="center"
            android:marqueeRepeatLimit="marquee_forever"
            android:padding="5dp"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:text="IMEI"
            android:textColor="#fff"
            android:textSize="15sp"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/connection_type"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@color/white"
            android:gravity="center"
            android:hint=""
            android:padding="10dp"
            android:textColor="#ff669900"
            android:textSize="15sp" />
    </LinearLayout>

Out Put look like this :-

How to put an EditText and Button next to each other?

May be this will help ,you can change TextView to Button or change position all is up to you.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜