2016-02-12 59 views
0

我想要兩個文本和一個箭頭標記到最後。所以,我試過如下:Android中使用線性佈局時的佈局問題

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    android:layout_height="wrap_content"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:orientation="horizontal" 
     android:layout_weight="0.25" 
     android:gravity="center_vertical"> 

     <TextView 
      android:id="@+id/labeltext" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:text="Specification"/> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:gravity="right" 
      android:src="@drawable/ic_next"/> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

    <TextView 
      android:id="@+id/labelselected" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:text="Specification down"/> 

    </LinearLayout> 
</LinearLayout> 

但我仍然得到:

enter image description here

我想如果我們把安卓重力=「正確」的整體形象進入的權利,但這個沒不動?

有人可以幫我解決這個問題嗎?

+0

集重力到右的TextView –

+0

變化方向報的佈局「水平「 – PeDuCKA

+0

它將layout_gravity不只是gra vity –

回答

3

注:

  1. 先放LinearLayouthorizontal方向

  2. 然後把另外2 LinearLayout裏面並設置layout_weight爲你的慾望

  3. 再加入什麼你想要在裏面。

多數民衆贊成....

這裏是更正後的代碼

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:orientation="vertical" 
android:layout_height="wrap_content"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="4" 
    > 
    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:gravity="center_vertical" 
     android:layout_weight="3"> 

     <TextView 
      android:id="@+id/labeltext" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:text="Specification" /> 


     <TextView 
      android:id="@+id/labelselected" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:text="Specification down"/> 


    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" 
     android:layout_weight="1" 
     android:gravity="center"> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:gravity="right" 
      android:src="@android:drawable/sym_def_app_icon"/> 

    </LinearLayout> 
</LinearLayout> 

enter image description here

+0

謝謝!這工作。 – user5287166

2

你可以這樣做。

截圖參考:

enter image description here

我做到了通過設置match_parent寬度TextView和分配drawableRight屬性。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.25" 
     android:gravity="center_vertical" 
     android:orientation="horizontal"> 

     <TextView 
      android:id="@+id/labeltext" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:drawableRight="@drawable/ic_next" 
      android:gravity="center_vertical" 
      android:text="Specification" /> 


    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/labelselected" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:text="Specification down" /> 

    </LinearLayout> 
</LinearLayout> 

希望這會幫助你。

1

而不是使用ImageView可以作爲繪製你的圖標設置爲您TextView其寬度設置爲match_parent

<TextView 
... 
android:drawableRight="@drawable/your_icon"/> 

的也是我覺得你的IDE會建議您刪除您LinearLayoutTextViewImageView和使用我的解決方案,因爲使用一個部件而不是三個總是更好;)

0

使用FrameLayout而不是LinearLayout像這裏:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:orientation="vertical" 
      android:layout_height="wrap_content"> 

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.25" 
    android:gravity="center_vertical"> 

    <TextView 
     android:id="@+id/labeltext" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:text="Specification"/> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:gravity="right" 
     android:src="@drawable/ic_next" 
     android:layout_gravity="right|center_vertical"/> 

</FrameLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/labelselected" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:text="Specification down"/> 

</LinearLayout> 

1

更改您的佈局和嘗試。我只是提示。將文本和圖像放置在分佈式layout_weight的同一行中。您可以根據需要進行調整

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    android:layout_height="wrap_content"> 

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

     <TextView 
      android:id="@+id/labeltext" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="3" 
      android:text="Specification"/> 

     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="3" 
      android:src="@drawable/ic_next"/> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

    <TextView 
      android:id="@+id/labelselected" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:text="Specification down"/> 

    </LinearLayout> 
</LinearLayout> 
1

將此替換爲您的上部佈局。您需要爲imageView使用layout_gravity =「right」。

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

     <TextView 
      android:id="@+id/labeltext" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:gravity="left|center" 
      android:text="Specification"/> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:gravity="right" 
      android:layout_gravity="right" 
      android:src="@drawable/ic_next"/> 

    </LinearLayout> 
0

只是爲了lateltext TextView中添加重量1

<TextView 
     android:id="@+id/labeltext" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:gravity="left|center_vertical" 
     android:text="Specification" 
     android:layout_weight="1"/> 
1

使用此它會爲你工作。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:id="@+id/labeltext" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Specification" /> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:src="@drawable/ic_next" /> 

    </RelativeLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/labelselected" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:text="Specification down" /> 

    </LinearLayout> 
</LinearLayout>