2015-01-08 54 views
2

我正在嘗試將spinner/progress bar添加到TextView左側將微調器/進度條添加到文本視圖中?

________________ 
| O MY TEXT | 
|________________| 

O = spinner/progress bar 

起初,我想到的是增加一個TextView,並在其頂部添加ProgressBar。是否有可能在TextView本身做到這一點?如果不是這樣做的最好方法是什麼?

<RelativeLayout android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 

     <TextView 
      android:id="@+id/latlongLocation" 
      android:layout_width="150dp" 
      android:layout_height="23dp" 
      android:layout_alignParentBottom="true" 
      android:layout_centerHorizontal="true" 
      android:layout_marginBottom="21dp" 
      android:background="@drawable/black" 
      android:gravity="center" 
      android:singleLine="true" 
      android:textColor="#202020" 
      android:textSize="11sp" /> 

     <RelativeLayout android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 

      <ProgressBar 
       android:id="@+id/progressBar1" 
       style="?android:attr/progressBarStyleLarge" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="true" 
       android:layout_centerHorizontal="true" /> 

     </RelativeLayout> 

    </RelativeLayout> 
+0

有你爲什麼在你的佈局硬編碼保證金和部件尺寸特殊的原因?您可以使用樣式/維度資源來保持應用程序的一致性。關於文本顏色或繪圖也是如此。 – 2Dee

+0

@ 2Dee我是Android SDK的新手,謹慎幫助我糾正我的代碼? –

回答

4

使用FrameLayout。有了它,你可以像卡片堆棧中放置視圖。

小例子:

<!-- Example of parent --> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="16dp" 
    android:orientation="vertical"> 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@android:color/black"> 

     <ProgressBar 
      style="@android:style/Widget.ProgressBar.Large" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="90dp" 
      android:layout_marginTop="20dp" 
      android:textSize="32dp" 
      android:textColor="@android:color/white" 
      android:text="Test text"/> 
    </FrameLayout> 
</LinearLayout> 

看起來像這樣

enter image description here

+0

真棒,但現在'TextView'和'ProgressBar'已經走到了左上角。 –

+0

你必須設置正確的邊距,等待,我會編輯答案 – vilpe89

+0

我看起來更像這樣:http://i.imgur.com/I10p8GD.png –