Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用styleable_Android_Android Layout_Declare Styleable_Styleable - Fatal编程技术网

Android 使用styleable

Android 使用styleable,android,android-layout,declare-styleable,styleable,Android,Android Layout,Declare Styleable,Styleable,我想创建一个包含进度视图和按钮的视图。通过视图的xml,我想添加定义按钮和porgress条样式的字段 到目前为止,我所做的一切都不起作用: <io.**.**.view.ButtonLoading android:id="@+id/b_recover_password" android:layout_width="wrap_content"

我想创建一个包含进度视图和按钮的视图。通过视图的xml,我想添加定义按钮和porgress条样式的字段

到目前为止,我所做的一切都不起作用:

   <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"/>
可设置样式:

   <declare-styleable name="ButtonLoading">
    <attr name="styleButton" format="reference" />
    <attr name="styleProgress" format="reference" />
    <attr name="textButton" format="string" />
</declare-styleable>


有人帮我吗?谢谢

问题出在
按钮
进度条
的构造函数中。让我们考虑两个构造函数。(重点是我的。)

视图(上下文、属性集属性、int-defStyleAttr)[适用于以下API 21]

从XML执行扩展,并从主题属性应用特定于类的基本样式。视图的构造函数允许子类在膨胀时使用自己的基本样式。例如,Button类的构造函数将调用此版本的超类构造函数,并为defStyleAttr提供R.attr.buttonStyle;这允许主题的按钮样式修改所有基本视图属性(特别是其背景)以及按钮类的属性

视图(上下文上下文、属性集属性、int-defstylettr、int-defStyleRes)[适用于API 21+]

从XML执行扩展,并从主题属性或样式资源应用特定于类的基本样式。视图的构造函数允许子类在膨胀时使用自己的基本样式

关注最后两个论点

defStyleAttr int:当前主题中的一个属性,其中包含对样式资源的引用,该资源为视图提供默认值。可以为0以不查找默认值

defStyleRes int:样式资源的资源标识符,为视图提供默认值,仅当defStyleAttr为0或在主题中找不到时使用。可以为0以不查找默认值

这是令人困惑的,因为有资源ID指向资源ID,但请耐心听我说

您为
buttonId
progressId
定义的是指向样式的资源id(
我的样式是:

    <!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:buttonStyle">@style/ButtonAppTheme</item>
    <item name="android:progressBarStyle">@style/ProgressBarBase</item>
</style>

<style name="ProgressBarBase" parent="android:Widget.Holo.Light.ProgressBar">
    <item name="android:textColorTertiary">@color/color_grey</item>
</style>

<style name="ProgressBar" parent="android:Widget.Holo.Light.ProgressBar">
    <item name="android:textColorTertiary">@color/color_black</item>
</style>

@颜色/原色
@颜色/原色暗
@颜色/颜色重音
@风格/主题
@风格/进度
@颜色/颜色/灰色
@颜色/颜色/黑色

问题是您总是使用“ProgressBarBase”样式。也就是说,它不是重写的。

当您说“它不工作”时,这到底是什么意思?按钮和progressbar上的样式集不工作。请坚持默认样式。此代码在自定义视图中。它在该视图中是什么方法。基(扩展)是什么视图。你能为进度条和按钮发布你的样式吗?
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="styleButton">@style/ButtonGrey</item>
    ...
</style>        
public CustomViewGroup(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    TypedArray a = context.getTheme().obtainStyledAttributes(
            attrs,
            R.styleable.ButtonLoading,
            0, 0);
    int defStyleRes = 0;
    try {
        defStyleRes = a.getResourceId(R.styleable.ButtonLoading_styleButton, 0);
    } catch (Exception e) {
      // do something
    }
    Button button;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        // defStyleRes is only used if defStyleAttr == 0 
        // or can't be found in the current theme.
        button = new Button(getContext(), attrs, defStyleAttr, defStyleRes);
    } else {
        button = new Button(getContext(), attrs, 
                     (defStyleAttr != 0) ? defStyleAttr : R.attr.styleButton);
    }
    try {
        String value = a.getString(R.styleable.ButtonLoading_textButton);
        button.setText(value);
    } catch (Exception e) {
      // do something
    }
    addView(button);
    a.recycle();
}
    <!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:buttonStyle">@style/ButtonAppTheme</item>
    <item name="android:progressBarStyle">@style/ProgressBarBase</item>
</style>

<style name="ProgressBarBase" parent="android:Widget.Holo.Light.ProgressBar">
    <item name="android:textColorTertiary">@color/color_grey</item>
</style>

<style name="ProgressBar" parent="android:Widget.Holo.Light.ProgressBar">
    <item name="android:textColorTertiary">@color/color_black</item>
</style>