2013-04-13 139 views
2

我想讓按鈕圓形,隨機背景顏色?當我嘗試下面的代碼時,該按鈕不是圓形的。如何設置同時舍入的隨機顏色和按鈕?

下面是代碼:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.newtestament); 
    LinearLayout layout = (LinearLayout) findViewById(R.id.linearlayoutnew); 
    String[] values = { "Matthai", "Marka", "Luka", "Johan", 
      "Sawltak Tangthu", "Rom Laikhak", "Korin Masa", "Korin Nihna", 
      "Galati", "Efesa", "Filippi", "Kolose", "Thesalonika Masa", 
      "Thesalonika Nihna", "Timoti Masa", "Timoti Nihna", "Titus", 
      "Filemon", "Hebru", "James", "Peter Masa", "Peter Nihna", 
      "Johan Masa", "Johan Nihna", "Johan Thumna", "Jude", 
      "Maangmuhna" }; 
    Button[] b = new Button[values.length]; 
    for (int i = 0; i < b.length; i++) { 
     b[i] = new Button(this); 
    } 
    int[] btnColor = { 0xAAe60038, 0xAA9142d6, 0xAAf07b04, 0xAA1515ff, 
      0xAA23699e, 0xAA0a71ff, 0xAA3e3d39, 0xAA00b323, 0xAA754e45, 
      0xAAfa061e, 0xAAe66d2d, 0xAAff00ff }; 
    // calling random 
    Random random = new Random(); 
    // ramdomizing a color 
    int c = btnColor[random.nextInt(btnColor.length)]; 
    for (int i = 0; i < values.length; i++) { 

     // applying button rounded xml style 
     b[i].setBackgroundDrawable(getResources().getDrawable(
       R.drawable.round)); 
     // setting randomized color 
     b[i].setBackgroundColor(c); 
     // layout 
     LayoutParams params = new LinearLayout.LayoutParams(
       LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
     // margin 
     ((MarginLayoutParams) params).setMargins(5, 5, 5, 5); 
     // padding 
     b[i].setPadding(10, 10, 10, 10); 
     // text color 
     b[i].setTextColor(0xFFFFFFFF); 
     // text 
     b[i].setText(values[i]); 
     // text size 
     b[i].setTextSize(18); 
     Typeface face = Typeface.createFromAsset(getBaseContext() 
       .getAssets(), "UBUNTU-R.TTF"); 
     b[i].setTypeface(face); 
     layout.addView(b[i], params); 

    } 

} 

這裏是round.xml:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" > 

<!-- you can use any color you want I used here gray color --> 
<corners 
    android:bottomLeftRadius="5dp" 
    android:bottomRightRadius="5dp" 
    android:topLeftRadius="5dp" 
    android:topRightRadius="5dp" /> 
    </shape> 

我認爲問題是setBackgroundDrawable,setBackgroundColor和romdomizing。需要哪些附加代碼? ???
更新:如何編輯round.xml以使按鈕變圓?

回答

2

請看Documentview.setBackgroundDrawable(drawable)b[i].setBackgroundColor(c);你不能同時使用兩者。它只會影響到最後一個。

+0

是什麼答案究竟是什麼意思?我只是初學者。請清楚解釋我。 :-( –

+1

而不是使用setBackgroundDrawable,只是使用setBackgroundResource(); –

+0

我可以編輯round.xml來使按鈕圓整嗎? –

3

您將按鈕背景設置爲可繪製,然後顏色也爲。所以語句按順序執行。所以最後你的按鈕應該有一個背景顏色集。 因此,請嘗試評論其中一個並再次運行代碼。

b[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.round)); 
    // set background drawable 
    // first your background drawable will be set 
    b[i].setBackgroundColor(c); 
    //set background color. 

bkg.xml在繪製文件夾

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_pressed="true" 
    android:drawable="@drawable/pressed" /> 
<item android:state_focused="false" 
    android:drawable="@drawable/normal" /> 
</selector> 

normal.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="#FFFFFF"/>  
<stroke android:width="3dp" 
     android:color="#0FECFF" /><!-- #330000FF #ffffffff --> 

<padding android:left="5dp" 
     android:top="5dp" 
     android:right="5dp" 
     android:bottom="5dp"/> 
<corners android:bottomRightRadius="7dp" 
     android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp" 
     android:topRightRadius="7dp"/> 
</shape> 

pressed.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="#FF1A47"/>  
<stroke android:width="3dp" 
     android:color="#0FECFF"/> 
<padding android:left="5dp" 
     android:top="5dp" 
     android:right="5dp" 
     android:bottom="5dp"/> 
<corners android:bottomRightRadius="7dp" 
     android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp" 
     android:topRightRadius="7dp"/> 
</shape> 

    b[i].setBackgroundResource(R.drawable.bkg); 

快照

enter image description here

編輯:

int colors []= {Color.RED, Color.BLACK, Color.BLUE,Color.GREEN}; 
    Random r = new Random(); 
    for (int i = 0; i < colors.length; i++) { 
     b[i].setBackgroundColor(colors[r.nextInt(3)]); 
    } 
+0

其中是bkg.xml ??? –

+0

你必須創建一個文件夾在res/drawable下繪製,並把你所有的xmls放在上面可繪製的文件夾 – Raghunandan

+0

可以編輯round.xml來使按鈕變圓? –

相關問題