Android有没有一种方法可以设计所有小部件的样式?
嗨,伙计们,我对android布局的样式还不熟悉,我想问一下,是否有一种方法可以将可绘制背景应用到一个小部件(例如:布局中的所有按钮),而不必在每个小部件中键入android:drawable 您可以创建类扩展按钮:Android有没有一种方法可以设计所有小部件的样式?,android,android-studio,Android,Android Studio,嗨,伙计们,我对android布局的样式还不熟悉,我想问一下,是否有一种方法可以将可绘制背景应用到一个小部件(例如:布局中的所有按钮),而不必在每个小部件中键入android:drawable 您可以创建类扩展按钮: public class CustomButton extends Button { public CustomButton(Context context, AttributeSet attrs) { super(context, attrs);
public class CustomButton extends Button {
public CustomButton(Context context, AttributeSet attrs) {
super(context, attrs);
//set drawable here
}
}
在xml文件中调用CustomButton:
<yourpackage.name.CustomButton
android:id="@+id/xxx"
android:text="CustomButton" />
您可以在每个布局中添加它,但也可以通过迭代页面上的每个控件以编程方式添加它。我最近做了这件事。我不喜欢这个结果,因为不可能(字面上)检查现有的样式以确定是否需要替换它-我不想重新设置标签样式-但它是这样的:
//change linerlayout to whatever viewgroup type you have
LinearLayout layout = (LinearLayout) findViewById(R.id.name_of_your_linearlayout);
for(int count = 0; count < layout.getChildCount(); count ++) {
if (layout.getChildAt(count)) instanceOf EditText) {
/*do your work here. Note you don't have to limit yourself
to theming. You could add an event to every button or textbox
at the same time */
}
}
//将linerlayout更改为您拥有的任何视图组类型
LinearLayout layout=(LinearLayout)findViewById(R.id.name\u of\u your\u LinearLayout);
对于(int count=0;count
我更喜欢这种方法。多谢各位^-^