【发布时间】:2017-08-10 14:40:39
【问题描述】:
我想制作一个包含进度视图和按钮的视图。通过视图的 xml,我想添加定义按钮和入口栏样式的字段。
到目前为止我做了什么,但它不起作用:
<io.**.**.view.ButtonLoading android:id="@+id/b_recover_password"
android:layout_width="wrap_content"
android:gravity="center"
app:styleButton="@style/ButtonGrey"
app:styleProgress="@style/ProgressBar"
app:textButton="@string/recover_password"
android:layout_height="wrap_content"/>
代码:
a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.ButtonLoading,
0, 0);
int buttonId = 0;// R.style.Widget_AppCompat_Button;
try {
buttonId = a.getResourceId(R.styleable.ButtonLoading_styleButton, 0);
} catch (Exception e) {
}
button = new Button(getContext(), attrs, buttonId);
LayoutParams lpButton = createLayoutParams();
button.setLayoutParams(lpButton);
color = button.getCurrentTextColor();
int progressId = 0;// R.style.Widget_AppCompat_ProgressBar;
try {
progressId = a.getResourceId(R.styleable.ButtonLoading_styleProgress, 0);
} catch (Exception e) {
}
progressBar = new ProgressBar(getContext(), attrs, progressId);
LayoutParams lpProgressBar = createLayoutParams();
lpProgressBar.addRule(RelativeLayout.CENTER_IN_PARENT);
progressBar.setLayoutParams(lpProgressBar);
LayoutParams lpRelativeLayout = createLayoutParams();
setLayoutParams(lpRelativeLayout);
addView(button);
addView(progressBar);
try {
String value = a.getString(R.styleable.ButtonLoading_textButton);
button.setText(value);
} catch (Exception e) {
}
a.recycle();
风格化:
<declare-styleable name="ButtonLoading">
<attr name="styleButton" format="reference" />
<attr name="styleProgress" format="reference" />
<attr name="textButton" format="string" />
</declare-styleable>
有人帮助我吗?谢谢
【问题讨论】:
-
当您说“它不起作用”时,这究竟是什么意思?
-
按钮和进度条上的样式集不起作用。坚持使用默认样式。
-
此代码在自定义视图中。在那个视图中它是什么方法。什么是基本(扩展)视图。您可以发布进度条和按钮的样式吗?
标签: android android-layout declare-styleable styleable