简体   繁体   中英

How to align a TextView, Spinner and Button in same row in android

How to align a TextView, Spinner and Button in same row in android ?

My layout xmls as follows

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"  
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" 
android:gravity="center">


<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
     >

    <TextView android:id="@+id/textView1" 
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:text="Show"
        android:gravity="center" 
        android:layout_weight="1"
        android:textColor="#CD2134"  
        android:textStyle="bold"  />


    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:layout_marginBottom="15dp"
        android:prompt="@string/prompt"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Get"
        android:layout_weight="1"
        android:onClick="button_click"/>


</LinearLayout>


  </LinearLayout>

output of the above layout as follows

在此处输入图片说明

I want just like below image

在此处输入图片说明

thanks in advance

Try this..

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Show"
        android:textColor="#CD2134"
        android:textStyle="bold" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:prompt="@string/prompt" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="button_click"
        android:text="Get" />
</LinearLayout>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:text="Show"
    android:textColor="#CD2134"
    android:textStyle="bold" />

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_gravity="center_vertical"
    android:prompt="@string/prompt" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="button_click"
    android:layout_gravity="center_vertical"
    android:text="Get" />

Try this

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"  
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" 
android:gravity="center">

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:gravity="center"
    android:text="Show"
    android:textColor="#CD2134"
    android:textStyle="bold" />

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_marginBottom="15dp"
/>

<Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:onClick="button_click"
    android:text="Get" />

Hope this will solve the issue

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:weightSum="3"
android:orientation="horizontal" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" 
   />

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1" 
  />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" 
    android:text="Get" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM