gpt4 book ai didi

java - Android - Java 中的 "android:theme="是什么?

转载 作者:行者123 更新时间:2023-11-29 19:42:20 24 4
gpt4 key购买 nike

假设我有这个布局 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 来构造您的 View ,那么您将使用指定的主题。

Context themedContext = new ContextThemeWrapper(context, R.style.orange_theme);
CustomButton submit = new CustomButton(themedContext);

关于java - Android - Java 中的 "android:theme="是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38447969/

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