簡體   English   中英

如何在Android的同一行中對齊TextView,微調框和按鈕

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

如何在Android的同一行中對齊TextView,Spinner和Button?

我的布局xml如下

<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>

以上布局的輸出如下

在此處輸入圖片說明

我想要像下面的圖像

在此處輸入圖片說明

提前致謝

嘗試這個..

<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" />

嘗試這個

<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" />

希望這能解決問題

<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" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM