![](/img/trans.png)
[英]What is the Java (Android) equivalent for Cocoa's NSDateFormatter?
[英]Android - What's the equivalent of “android:theme=” in Java?
假设我有这个布局xml(由于selectableItemBackground而产生波纹的蓝色按钮):
<LinearLayout
android:id="@+id/yes_frame"
android:background="@color/Blue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:id="@+id/dialogCancelButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Cancel"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/White"
android:background="?attr/selectableItemBackground"
android:theme="@style/ripplePressed"
/>
</LinearLayout>
和themes.xml (使波纹颜色变为白色):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="ripplePressed">
<item name="android:colorControlHighlight">@color/White</item>
</style>
</resources>
在Java中模拟xml:
CustomButton submit = new CustomButton(context);
LinearLayout wrapperLinearLayout = new LinearLayout(context);
wrapperLinearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
wrapperLinearLayout.setBackgroundColor(Color.BLUE);
LinearLayout.LayoutParams layoutParamsButton = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
(int) Utils.convertDpToPixel(55, context));
//selectableItemBackground in Java
int[] attrs = new int[]{R.attr.selectableItemBackground};
TypedArray ta = context.obtainStyledAttributes(attrs);
int backgroundResource = ta.getResourceId(0, 0);
submit.setBackgroundResource(backgroundResource);
ta.recycle();
submit.setLayoutParams(layoutParamsButton);
submit.setTextColor(Color.WHITE);
submit.setText("some text");
wrapperLinearLayout.addView(submit);
Java代码工作正常,除非我无法弄清楚Java中的等效android:theme="@style/ripplePressed
。
我认为,如果要使用ContextThemeWrapper
构造视图,则可以使用指定的主题。
Context themedContext = new ContextThemeWrapper(context, R.style.orange_theme);
CustomButton submit = new CustomButton(themedContext);
您可以使用ContextThemeWrapper
类
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.