2013-10-18 60 views
1

晚上好。LinearLayout中的LinearLayout包含3個元素 - 一個指向左側,一個指向中心,另一個指向右側

我擺弄這一個多小時,現在我沒有得到它的工作。 我想創建一個版面看起來應該像這樣的:

enter image description here

我想有神韻:向左一個TextView,中心的進度和神韻:向右另一個TextView的。我不想使用靜態寬度,因爲佈局應該用於不同的設備。

不幸的是我的佈局看起來像這樣的時刻: enter image description here

我沒有找到通過谷歌的任何解決方案,因此任何幫助非常感謝。 這是我的佈局:

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

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

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

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="25sp" 
      android:layout_marginRight="5sp" 
      android:gravity="center" 
      android:text="0 %" /> 

     <ProgressBar 
      android:id="@+id/progressBar1" 
      style="?android:attr/progressBarStyleHorizontal" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.36" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_gravity="right" 
      android:layout_marginLeft="5sp" 
      android:layout_marginRight="25sp" 
      android:gravity="right" 
      android:text="100 %" /> 
    </LinearLayout> 

</LinearLayout> 

回答

2

你可以做的很好嘗試RelativeLayout像這樣:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity"> 

    <TextView 
     android:id="@+id/yourTitle" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:textSize="20pt" 
     android:text="TextView" /> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/yourTitle"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="25sp" 
      android:layout_marginRight="5sp" 
      android:gravity="center" 
      android:text="0 %" /> 

     <ProgressBar 
      android:id="@+id/progressBar1" 
      style="?android:attr/progressBarStyleHorizontal" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.36" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_gravity="right" 
      android:layout_marginLeft="5sp" 
      android:layout_marginRight="25sp" 
      android:gravity="right" 
      android:text="100 %" /> 
     </LinearLayout> 

</RelativeLayout> 

將會產生這樣的:

Image of layout from emulator

1

我剛將weight添加到文本視圖並設置它們的width 0dp

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

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

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

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="25sp" 
      android:layout_marginRight="5sp" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="0 %" /> 

     <ProgressBar 
      android:id="@+id/progressBar1" 
      style="?android:attr/progressBarStyleHorizontal" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="5" /> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_gravity="right" 
      android:layout_marginLeft="5sp" 
      android:layout_marginRight="25sp" 
      android:layout_weight="1.2" 
      android:gravity="right" 
      android:text="100 %" /> 
    </LinearLayout> 

</LinearLayout> 
1

我試過你的代碼,它對我來說工作正常。但您可以嘗試在ProgressBar中將android:layout_width改爲0dip。

<ProgressBar 
    android:id="@+id/progressBar1" 
    style="?android:attr/progressBarStyleHorizontal" 
    android:layout_width="0dip" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" /> 
相關問題