Android Styles: Difference between 'style=“@an

2019-02-02 13:12发布

I'm trying to style buttons to look like those I ask about in Android Full Width ICS style Minimalist Bottom ButtonsViews.

I've succeeded, with the following xml for anyone interested:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom"
        android:divider="@android:drawable/divider_horizontal_dark"
        android:gravity="bottom"
        android:orientation="vertical"
        android:paddingTop="16dip"
        android:showDividers="beginning|end" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:measureWithLargestChild="true"
            android:orientation="horizontal"
            android:divider="@android:drawable/divider_horizontal_dark"
            android:showDividers="middle" >                             


            <Button
                android:id="@+id/cancel_button"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:layout_weight="1"
                android:maxLines="2"
                android:text="@string/cancel_button" />

            <Button
                android:id="@+id/login_button"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_weight="1"
                android:filterTouchesWhenObscured="true"
                android:maxLines="2"
                android:text="@string/login_button" />

        </LinearLayout>
    </LinearLayout>

One question though. The eclipse content assist has no idea what's happening with the following resource resolution:

style="?android:attr/buttonBarButtonStyle"

I'm familiar with the typical resolution (which eclipse's content assist is aware of)

style=@android/style/...

...but I'm not clear on the difference between the two. It seems like some style attributes appear in one but not the other. For instance, the following doesn't resolve to anything:

style=@android:attr/buttonBarStyle

and neither does this:

style="@android:style/buttonBarStyle

So I guess two questions here:

  1. Why the difference in resource referencing syntax?
  2. Why the confusing miscategorization of styles in under an attr category.
  3. What is the attr category even used for again?

Thanks!

2条回答
一纸荒年 Trace。
2楼-- · 2019-02-02 13:30

From another answer on SO (paraphrased here):

Using a question mark in front of an ID means that you want to access a style attribute that's defined in a style theme, rather than hardcoding the attribute.

Think of it as dereferencing a theme attribute to fetch the resource it points to rather than referring to the attribute itself.

Referencing Style Attributes

查看更多
戒情不戒烟
3楼-- · 2019-02-02 13:47

?android:attr/buttonBarButtonStyle or just ?android:buttonBarButtonStyle - (attr is optional)

<item name="buttonBarButtonStyle">@android:style/Widget.Button</item>

This internally dereferences to @android:style/Widget.Button which is defined within appcompat/res/values/themes.xml file.

(?android:) - always links to the value of the attribute from the current-theme applied. Thus allowing us to refer the android defined style only rather creating a new one and supplying a hard-coded value.

Google Doc says, "It uses the style that is defined by this attribute, in the current theme"

查看更多
登录 后发表回答