gpt4 book ai didi

java - 如何同时设置随机颜色和圆形按钮?

转载 作者:太空狗 更新时间:2023-10-29 16:42:09 26 4
gpt4 key购买 nike

我正在尝试使按钮变圆,随机背景颜色?当我尝试下面的代码时,按钮没有变圆。

代码如下:

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使按钮变圆?

最佳答案

您正在将按钮背景设置为可绘制,然后还设置颜色。所以语句是按顺序执行的。所以最后你的按钮应该有一个背景颜色集。因此,请尝试注释其中一个并再次运行代码。

    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>

普通.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>

按下.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)]);
}

关于java - 如何同时设置随机颜色和圆形按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15986445/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com