我正在尝试使用像这样的样式的TextView构造函数:
TextView myText = new TextView(MyActivity.this, null, R.style.my_style );
但是,当我这样做时,文本视图似乎不采用样式(我通过在静态对象上设置它来验证样式).
我也尝试过使用myText.setTextAppearance(MyActivity.this, R.style.my_style)
但它也不起作用
我通过一些按钮以编程方式为AlertDialog创建LinearLayout.
我WANT做到这一点:
<LinearLayout android:id="@+id/footer" android:layout_width="fill_parent"
style="@android:style/ButtonBar">
Run Code Online (Sandbox Code Playgroud)
但是使用这样的代码:
LinearLayout buttons = new LinearLayout(parentContext);
buttons.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams buttonsParams =
new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
topLayout.addView(buttons, buttonsParams);
buttons.setLayoutParams(buttonsParams);
Button btnAdd = new Button(context);
btnAdd.setText("Add");
Run Code Online (Sandbox Code Playgroud)
如何以编程方式设置按钮的样式(使用按钮栏)?
我开始使用a ContextThemeWrapper
来动态地应用一个样式ImageButton
; 基于对我的另一个问题的回答,以及对其他类似 问题的回答.
ContextThemeWrapper wrapper = new ContextThemeWrapper(getContext(), mStyleRes);
mImageButton = new AppCompatImageButton(wrapper, null, 0);
Run Code Online (Sandbox Code Playgroud)
但最近一个lint错误开始出现在ContextThemeWrapper
构造函数上:
ContextThemeWrapper
只能从同一个库组中调用(groupId = com.android.support)
我注意到标有@RestrictTo(LIBRARY_GROUP)
注释的类,这会导致lint错误出现.但是我找不到任何关于它为什么仅限于com.android.support
图书馆组的信息.
据我所知,这是以View
编程方式应用样式,主题或主题叠加的唯一方法; 除了将默认样式属性设置为构造函数中的第三个参数之外.所以我想知道为什么它的使用会受到限制; 在支持库之外使用类有什么问题吗?可能会有我不知道的副作用吗?
我遇到的唯一类似的问题是关于(现在已修复)的错误; 导致此lint错误显示在onCreate
子类的方法上AppCompatActivity
.我认为这不是一个错误,而是一个故意的限制; 我想知道背后的推理.
我应该注意; 这个限制(截至目前)实际上似乎对使用a的代码没有影响ContextThemeWrapper
.它编译并运行良好,并按照我的预期工作.
android syntax-error android-context android-lint android-support-library
有没有一种方法可以在Android中以编程方式更改TextInputLayout的主题。如果我有以下TextInputLayout例如:
<android.support.design.widget.TextInputLayout
android:id="@+id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:paddingTop="16dp"
android:theme="@style/TextInputLayoutTheme"
app:errorTextAppearance="@style/Error">
<android.support.v7.widget.AppCompatEditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="8dp"
android:paddingTop="8dp"/>
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
我可以以android:theme="@style/TextInputLayoutTheme"
编程方式将此行更改为另一个主题吗?
我正在尝试创建一个自定义视图,从该视图扩展MaterialButton
并在代码中应用样式,因此我不需要在xml中进行操作。
class CustomRedButton @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : MaterialButton(ContextThemeWrapper(context, R.style.ButtonRedStyle), attrs, defStyleAttr)
Run Code Online (Sandbox Code Playgroud)
风格是:
<style name="ButtonRedStyle"
parent="Widget.MaterialComponents.Button.TextButton">
<item name="backgroundTint">@color/red</item>
<item name="rippleColor">@color/grey</item>
<item name="strokeWidth">1dp</item>
<item name="strokeColor">@color/black</item>
</style>
Run Code Online (Sandbox Code Playgroud)
一切正常,但backgroundTint
财产。由于某种原因,背景颜色没有改变,并且具有主题的原色。但是,如果我尝试将样式应用于MaterialButton
xml中,则会更改颜色。
知道为什么会发生或如何实现吗?