2012-07-10 38 views
0

我試圖創建一個更好看的按鈕欄爲我的應用程序來匹配主題。 到目前爲止,我使用下面的代碼:三個按鈕RelativeLayout與視圖分隔線

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/footer" 
    style="@android:style/ButtonBar" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="bottom" 
    android:orientation="horizontal" > 

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

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

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

,它給了我這樣的結果: enter image description here

我也在工作的一些代碼有更好看,以取代酒吧一。這是因爲如下:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="48dp" 
    android:layout_alignParentBottom="true" > 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="1dip" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="4dip" 
     android:layout_marginRight="4dip" 
     android:background="@drawable/black_white_gradient" /> 

    <View 
     android:id="@+id/ViewOne" 
     android:layout_width="1dip" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="4dip" 
     android:layout_marginTop="4dip" 
     android:background="@drawable/black_white_gradient" /> 

    <Button 
     android:id="@+id/ButtonOne" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_toLeftOf="@id/ViewOne" 
     android:background="@color/button_blue_background" 
     android:paddingTop="5dp" 
     android:text="Cancel" 
     android:textColor="@color/button_dark_text" /> 

    <Button 
     android:id="@+id/ButtonTwo" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_toRightOf="@id/ViewOne" 
     android:background="@color/button_blue_background" 
     android:text="OK" 
     android:textColor="@color/button_dark_text" /> 
</RelativeLayout> 

導致: enter image description here

所以我的問題是,我無法獲得新的黑條上有三個按鍵。我試着用android:layout_toRightOf屬性添加更多的視圖和按鈕,但它仍然不起作用。 有什麼想法可以做到這一點?

回答

1

您不需要將視圖添加到佈局以顯示您的漸變。

我建議你用這三個按鈕,使用您的的LinearLayout並設置層列表 XML作爲佈局本身的背景。

  1. 創建圖層列表的XML文件,例如layerlist.xml與梯度:

    <?xml version="1.0" encoding="utf-8"?> 
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
        <item 
        android:drawable="@drawable/gradient1" 
        android:id="@+id/grad1" 
        /> 
    
        <item 
        android:drawable="@drawable/gradient2" 
        android:id="@+id/grad2" 
        /> 
    </layer-list> 
    

    如果你只有一個梯度,那麼你並不需要一個層列表XML文件。將其直接設置爲LinearLayout的背景。

  2. 正如我前面所說,設置layerlist.xml(或單梯度XML文件)作爲佈局的背景:

    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/footer" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:gravity="bottom" 
        android:orientation="horizontal" 
        android:background="@drawable/layerlist" > 
    
        <Button 
         android:id="@+id/Formula" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1" 
         android:text="Formula" /> 
    
        <Button 
         android:id="@+id/Solve" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1" 
         android:text="Solve" /> 
    
        <Button 
         android:id="@+id/Clear" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_weight="1" 
         android:text="Clear" /> 
    </LinearLayout> 
    

讓我知道,如果它的工作原理!

編輯:

據我所知,你可以定義一個適當高度的梯度。將android:top="3dp"添加到您的layerlist.xml文件所需的項目。這意味着您的漸變將從頂部開始具有3dp的高度。 或者,如果您使用的是單梯度文件,你可以定義grandient的高度添加到您的形狀:

<size 
     android:height="3dp" /> 
+0

我喜歡你的解決方案,但它並沒有真正的工作。 這是發生了什麼:http://i49.tinypic.com/2ezpvlj.png 由於我的按鈕是透明的,直到點擊和漸變看起來相當奇怪。 – Pkmmte 2012-07-10 23:56:30

+0

理想是讓按鈕上方的水平線漸變,對嗎?漸變呈現所有的LinearLayout高度,但您可以定義漸變的正確高度,看起來像2或3dp。 – yugidroid 2012-07-11 00:04:47

+0

我編輯了我的答案,請檢查出來。我認爲這可能會對你有所幫助。 – yugidroid 2012-07-11 00:15:18

0

對於「三個按鈕」的情況,你可以設置LinearLayour屬性android:weightSum="3"這應該會給你三個相同大小的按鈕。

+0

嘗試過,但日食給了我這樣的:錯誤:未找到包裏的屬性「layout_weightsum」資源標識符'android' – Pkmmte 2012-07-11 00:01:12

+0

我改變了它。對於那個很抱歉。 – BlackHatSamurai 2012-07-11 00:04:55

+0

它仍然給我一個類似的錯誤。 我認爲這是因爲RelativeLayout不支持像LinearLayout這樣的權重。 – Pkmmte 2012-07-11 00:20:07