Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

xml - How can I extend a themed style attribute?

My progress bar uses a themed style:

<ProgressBar
    android:id="@+id/pbRegister"
    style="?android:attr/progressBarStyleHorizontal"   <---------
    android:indeterminate="true"
    android:visibility="invisible"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="0dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/etRegisterPassword2" />

Note the question mark at the front:

    ?android:attr/progressBarStyleHorizontal

If I remove the question mark like this:

    style="android:attr/progressBarStyleHorizontal

then my progress bar is quite different, so it is inheriting values from somewhere else based on my theme.

How can I find out all the details of what that style is inheriting from the theme and re-create it as 1 single style?

Essentially, I want to create a new style like this:

<style name="pbMyProgressBarStyle" parent="?android:attr/progressBarStyleHorizontal">
    <item name="android:indeterminate">true</item>
    <item name="android:visibility">invisible</item>
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_marginEnd">8dp</item>
    <item name="android:layout_marginLeft">8dp</item>
    <item name="android:layout_marginRight">8dp</item>
    <item name="android:layout_marginStart">8dp</item>
    <item name="android:layout_marginTop">0dp</item>
</style>

But I am not able to inherit from the parent style with a ? in it, and if I remove the ? I am missing some details of the style that I want.

question from:https://stackoverflow.com/questions/65644632/how-can-i-extend-a-themed-style-attribute

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ctrl + click on the style in the android studio to see the parent of that style.

Or you can simply do like this :

 <style name="customStyleName" parent="Widget.AppCompat.ProgressBar.Horizontal">
    .....
    //style your Progress Bar
    .....
 </style>

Or if you are using material theme :

 <style name="customStyleName" parent="Widget.MaterialComponents.LinearProgressIndicator">
   ...
 </style>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...