2016-03-01 57 views
1

所以我想風格LinearLayout看起來像一個按鈕。我使用LinearLayout,因爲我使用的「字體真棒」的圖標,我需要把圖標的按鈕裏面,所以我有一個LinearLayout,它有兩個TextView S作爲孩子。一個用於圖標,另一個用於文本。Android的風格查看喜歡的彩色按鈕

我要的是讓LinearLayout看起來像默認樣式的按鈕,但在不同的顏色。在棒棒糖和更新,它也應該有連鎖反應。

我設法做到這一點,通過添加一個backgroundTint,但它只適用於棉花糖,在KitKat該按鈕具有默認顏色。這裏是我的代碼:

佈局:

<LinearLayout 
     android:id="@+id/btn_register" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:clickable="true" 
     style="@style/MyTheme.Button" > 
     <TextView 
      android:id="@+id/ic_register" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="@android:color/white" 
      android:text="@string/fa_user_plus" 
      android:textSize="16dp" 
      android:paddingRight="4dp" /> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="@android:color/white" 
      android:text="Register as a new user" 
      android:textSize="16dp" /> 
    </LinearLayout> 

風格:

<style name="MyTheme.Button" parent="@android:style/Widget.DeviceDefault.Button"> 
    <item name="android:backgroundTint">@color/accent</item> 
</style> 
+0

嘗試採用了android:背景,而不是機器人的:backgroundTint – Joakim

+0

@Joakim,消除所有的按鈕風格 - 波紋和圓角。 –

+0

也許你可以使用一個簡單的按鈕,圖標作爲背景,並將你的文本放置在你想要的layout_align中。這比佈局更容易,但我不知道是否對你有用。 –

回答

0

解決方案:我創建2個不同的背景選擇器 - 一個用於API 21,一個用於舊設備。

繪製/ btn_red.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_pressed="true"> 
     <shape android:shape="rectangle"> 
      <solid android:color="#88FFFFFF" /> 
      <corners android:radius="2dp"/> 
     </shape> 
    </item>   

    <item> 
     <shape android:shape="rectangle"> 
      <solid android:color="@color/accent" /> 
      <corners android:radius="2dp"/> 
     </shape> 
    </item> 
</selector> 

繪製-V21/btn_red.xml:

<ripple 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:color="#88FFFFFF"> <!-- The ripple color --> 

    <!-- the normal bg color will be light white --> 
    <item> 
     <shape android:shape="rectangle"> 
      <solid android:color="@color/accent" /> 
      <corners android:radius="2dp"/> 
     </shape> 
    </item> 
</ripple> 
0

如果這是對你有用:

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="103dp" 
    android:layout_marginTop="46dp" 
    android:textColor="@android:color/white" 
    android:background="@drawable/your_icon" 
    android:gravity="right|center_horizontal" 
    android:text="Register as a new user" /> 

而且你可以自定義或改變它,你希望通過引力來改變文本的位置。

+0

不能使用圖標作爲背景,因爲它不是可繪製的。我使用「Font awesome」圖標字體來顯示圖標。 –

+0

啊,我明白了。如果你用文本繪製一個可繪製的東西,那麼簡單,用paint,photoshop ...和圖標呢?你可以做的按鈕,你可以和有一個真正的按鈕,您的喜好,文字,圖標... –

+0

然後我將不得不做出繪製的幾個版本不同的屏幕密度,我會做,對於每一個按鈕。 –