Jim*_*Jim 202 android android-layout
这是XML:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/LightStyle"
android:layout_width="fill_parent"
android:layout_height="55dip"
android:clickable="true"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
如何以style
编程方式设置属性?
Blu*_*ell 196
从技术上讲,您可以使用自定义视图以编程方式应用样式:
private MyRelativeLayout extends RelativeLayout {
public MyRelativeLayout(Context context) {
super(context, null, R.style.LightStyle);
}
}
Run Code Online (Sandbox Code Playgroud)
一个参数构造函数是以编程方式实例化视图时使用的构造函数.
因此,将此构造函数链接到带有样式参数的super.
RelativeLayout someLayout = new MyRelativeLayout(new ContextThemeWrapper(this,R.style.RadioButton));
Run Code Online (Sandbox Code Playgroud)
或者@Dori指出:
RelativeLayout someLayout = new RelativeLayout(new ContextThemeWrapper(activity,R.style.LightStyle));
Run Code Online (Sandbox Code Playgroud)
Ben*_*tte 109
什么对我有用:
Button b = new Button(new ContextThemeWrapper(this, R.style.ButtonText), null, 0);
Run Code Online (Sandbox Code Playgroud)
和
Kor*_*urk 86
您无法以编程方式设置视图的样式,但您可能会发现此线程很有用.
更新:在回答此问题时(2012年中,API级别14-15),以编程方式设置视图不是一种选择(即使存在一些非常重要的变通方法),而这是在更新的API之后实现的.版本.有关详细信息,请参阅@ Blundell的答案.
Alo*_*gan 14
对于新的Button/TextView:
Button mMyButton = new Button(new ContextThemeWrapper(this, R.style.button_disabled), null, 0);
Run Code Online (Sandbox Code Playgroud)
对于现有实例:
mMyButton.setTextAppearance(this, R.style.button_enabled);
Run Code Online (Sandbox Code Playgroud)
对于图像或布局:
Image mMyImage = new ImageView(new ContextThemeWrapper(context, R.style.article_image), null, 0);
Run Code Online (Sandbox Code Playgroud)
con*_*ull 13
这是一个很老的问题,但现在对我有用的解决方案是使用构造函数的第四个参数defStyleRes
- 如果可用......在视图中......设置样式
以下适用于我的目的(kotlin):
val textView = TextView(context, null, 0, R.style.Headline1)
Run Code Online (Sandbox Code Playgroud)
您可以通过执行以下操作将样式应用于您的活动:
super.setTheme( R.style.MyAppTheme );
Run Code Online (Sandbox Code Playgroud)
或Android默认值:
super.setTheme( android.R.style.Theme );
Run Code Online (Sandbox Code Playgroud)
在你的活动中,之前setContentView()
.
如果您想继续使用XML(接受的答案不允许您这样做)并在创建视图后设置样式,则可以使用支持所有可用属性子集的Paris库。
由于要从XML扩展视图,因此需要在布局中指定一个ID:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_styleable_relative_layout"
style="@style/LightStyle"
...
Run Code Online (Sandbox Code Playgroud)
然后,当需要膨胀版面之后,当需要以编程方式更改样式时:
// Any way to get the view instance will do
RelativeLayout myView = findViewById(R.id.my_styleable_relative_layout);
// This will apply all the supported attribute values of the style
Paris.style(myView).apply(R.style.LightStyle);
Run Code Online (Sandbox Code Playgroud)
有关更多信息:支持的视图类型和属性的列表(包括背景,填充,边距等,可以轻松扩展)以及带有附加文档的安装说明。
免责声明:我是上述图书馆的原作者。
提供的答案均不正确。
您可以通过编程设置样式。
长答案。这是我的片段,可通过编程方式为您的视图设置自定义样式:
1)在您的styles.xml文件中创建样式
<style name="MyStyle">
<item name="customTextColor">#39445B</item>
<item name="customDividerColor">#8D5AA8</item>
</style>
Run Code Online (Sandbox Code Playgroud)
不要忘记在attrs.xml文件中定义您的自定义属性
我的attrsl.xml文件:
<declare-styleable name="CustomWidget">
<attr name="customTextColor" format="color" />
<attr name="customDividerColor" format="color" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)
请注意,您可以为您的样式设置使用任何名称(我的CustomWidget)
现在,让我们将样式设置为小部件,这是我的简单小部件:
public class StyleableWidget extends LinearLayout {
private final StyleLoader styleLoader = new StyleLoader();
private TextView textView;
private View divider;
public StyleableWidget(Context context) {
super(context);
init();
}
private void init() {
inflate(getContext(), R.layout.widget_styleable, this);
textView = (TextView) findViewById(R.id.text_view);
divider = findViewById(R.id.divider);
setOrientation(VERTICAL);
}
protected void apply(StyleLoader.StyleAttrs styleAttrs) {
textView.setTextColor(styleAttrs.textColor);
divider.setBackgroundColor(styleAttrs.dividerColor);
}
public void setStyle(@StyleRes int style) {
apply(styleLoader.load(getContext(), style));
}
}
Run Code Online (Sandbox Code Playgroud)
布局:
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:layout_gravity="center"
android:text="@string/styleble_title" />
<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"/>
</merge>
Run Code Online (Sandbox Code Playgroud)
最后是StyleLoader类的实现
public class StyleLoader {
public StyleLoader() {
}
public static class StyleAttrs {
public int textColor;
public int dividerColor;
}
public StyleAttrs load(Context context, @StyleRes int styleResId) {
final TypedArray styledAttributes = context.obtainStyledAttributes(styleResId, R.styleable.CustomWidget);
return load(styledAttributes);
}
@NonNull
private StyleAttrs load(TypedArray styledAttributes) {
StyleAttrs styleAttrs = new StyleAttrs();
try {
styleAttrs.textColor = styledAttributes.getColor(R.styleable.CustomWidget_customTextColor, 0);
styleAttrs.dividerColor = styledAttributes.getColor(R.styleable.CustomWidget_customDividerColor, 0);
} finally {
styledAttributes.recycle();
}
return styleAttrs;
}
}
Run Code Online (Sandbox Code Playgroud)
您可以在https://github.com/Defuera/SetStylableProgramatically找到完整的示例
这是我的简单例子,关键是ContextThemeWrapper
wrapper,没有它,我的风格不行,使用View的三个参数构造函数。
ContextThemeWrapper themeContext = new ContextThemeWrapper(this, R.style.DefaultLabelStyle);
TextView tv = new TextView(themeContext, null, 0);
tv.setText("blah blah ...");
layout.addView(tv);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
226987 次 |
最近记录: |