ste*_*ler 2 java android android-alertdialog
我有一个警报对话框生成器,我在其中以编程方式定义布局。我有一个线性布局,我想为其设置一个属性,以便在运行时我可以更改应用程序的颜色主题。我已经完成了大部分工作,但我不知道如何进行线性布局,因为它没有在 xml 中定义。
我确实有一个十六进制颜色代码硬编码,但这不是我想要的。有没有办法设置像 ?attr/colorPrimary 这样的属性
alertAFFY = new AlertDialog.Builder(AddMakeActivity.this);
LinearLayout mainLayout = new
LinearLayout(AddMakeActivity.this);
mainLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout layoutTitle = new LinearLayout(AddAlarmActivity.this);
layoutTitle.setOrientation(LinearLayout.HORIZONTAL);
TextView title = new TextView(getApplicationContext());
title.setPadding(0, 30, 0, 30);
title.setTextColor(Color.parseColor("#FFFFFF"));
title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
title.setText("Select One");
layoutTitle.setGravity(Gravity.CENTER_HORIZONTAL);
layoutTitle.addView(title);
**// i need to change the background color to take in ?attr/ **
layoutTitle.setBackgroundColor(Color.parseColor("#F8B195"));
layoutTitle.setMinimumHeight(20);
mainLayout.addView(layoutTitle);
Run Code Online (Sandbox Code Playgroud)
我正在尝试访问主题属性
<style name="Theme1" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/toolbarColor</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorDays">@color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="windowNoTitle">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)
有没有办法为线性布局背景颜色设置属性?我需要它是动态的,这样我就可以在运行时更改它。它不能被硬编码在那里
尝试这个:
TypedValue typedValue = new TypedValue();
getApplicationContext().getTheme().resolveAttribute(android.R.attr.colorPrimary, typedValue, true);
// it's probably a good idea to check if the color wasn't specified as a resource
if (typedValue.resourceId != 0) {
layoutTitle.setBackgroundResource(typedValue.resourceId);
} else {
// this should work whether there was a resource id or not
layoutTitle.setBackgroundColor(typedValue.data);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1420 次 |
最近记录: |