2017-06-18 125 views
-1

我最近試圖爲我的朋友的應用程序構建一個用戶界面。 我有一些問題,不知道如何處理。 我已經搜索了一段時間的解決方案。 但仍然無法處理我的問題。Android:如何更改同一個按鈕中兩條不同線的顏色?

Here's what my apps looks like

我想知道是否有改變中間的按鈕列兩個文本的顏色的方法。 我想要的樣子是:數字是粉紅色的,中國人是灰色的,還有兩種不同的尺寸。

+1

你可以發表你的XML點上你的問題... –

+1

更改'Button'並使用'LinearLa yout'有兩個'TextViews',你可以根據需要自定義文本。 – Safa

回答

1

最好的選擇是將兩個文本分離爲兩個TextView,就像Safa在評論中提出的一樣,Elvira Parpalac回答。

如果你真的想保持兩種文本的一個按鈕,你可以使用SpannableStringForegroundColorSpan

String text1 = "xyz"; 
String text2 = "abc"; 
SpannableString spannable = new SpannableString(text1 + "\n" + text2); 
spannable.setSpan(new ForegroundColorSpan(Color.GREEN), 0, text1.length(), 0); 
spannable.setSpan(new ForegroundColorSpan(Color.RED), text1.length(), spannable.length(), 0); 

button.setText(spannable); 
1

您可以使用的LinearLayout自定義按鈕。您只需將android:clickable="true"屬性設置爲LinearLayout即可。這裏是你的情況的一個例子:

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

<LinearLayout 
    android:id="@+id/layout1" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:background="@drawable/button_selector" 
    android:clickable="true" 
    android:orientation="vertical" 
    android:padding="5dp"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="22" 
     android:textColor="@color/bp_disabled_day" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:gravity="center" 
     android:text="A" 
     android:textColor="@color/ampm_text_color" /> 

</LinearLayout> 

<View 
    android:layout_width="1dp" 
    android:layout_height="fill_parent" 
    android:background="@android:color/darker_gray" /> 

<LinearLayout 
    android:id="@+id/layout2" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:background="@drawable/button_selector" 
    android:clickable="true" 
    android:orientation="vertical" 
    android:padding="5dp"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="22" 
     android:textColor="@color/bp_disabled_day" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:gravity="center" 
     android:text="B" 
     android:textColor="@color/ampm_text_color" /> 

</LinearLayout> 

<View 
    android:layout_width="1dp" 
    android:layout_height="fill_parent" 
    android:background="@android:color/darker_gray" /> 

<LinearLayout 
    android:id="@+id/layout3" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:background="@drawable/button_selector" 
    android:clickable="true" 
    android:orientation="vertical" 
    android:padding="5dp"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="22" 
     android:textColor="@color/bp_disabled_day" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:gravity="center" 
     android:text="C" 
     android:textColor="@color/ampm_text_color" /> 

</LinearLayout> 
</LinearLayout> 

此外,您還需要添加這個文件drawable/button_selector.xml。在這個文件中,你將爲「按鈕」編寫樣式(當它處於不同的狀態時)。

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_longAnimTime"> 
<item android:drawable="@color/bpWhite" android:state_focused="false" android:state_pressed="false" /> 
<item android:drawable="@color/colorPrimaryDark" android:state_focused="true" android:state_pressed="true" /> 
<item android:drawable="@color/colorPrimaryDark" android:state_focused="false" android:state_pressed="true" /> 
<item android:drawable="@color/colorPrimaryDark" android:state_focused="true" android:state_pressed="false" /> 
</selector> 

而最後一步是設置OnclickListener在代碼:

LinearLayout layout1 = (LinearLayout) findViewById (R.id.layout1); 
layout1.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     //TODO: Write your code 
    } 
}); 

結果:

enter image description here

enter image description here

相關問題