How to refer android attribute layout_width from custom attribute?

岁酱吖の 提交于 2019-12-21 09:39:51

问题


I want to separate the usage of my application on xlarge devices and usage on other devices to restrict layout_width parameter by 720dp for xlarge. For this purpose I create values/attrs.xml:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <attr name="layoutWidth" format="reference|dimension" />
</resources>

with custom parameter layoutWidth to set it in

android:layout_width="?layoutWidth"

Furtner, I need to specify two themes.xml files for xlarge and ordinary devices in values-xlarge and values directories:

values-xlarge/themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme" parent="android:Theme">
        <item name="layoutWidth">720dp</item>
    </style>
</resources>

values/themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme" parent="android:Theme">
        <item name="layoutWidth">What should I write here!?</item>
    </style>
</resources>

So, how can I make a reference on Android "fill_parent" parameter at this place? It seems like @android:layout_width/fill_parent, but I have compiling Error:

 No resource found that matches the given name (at 'layoutWidth' with value '@android:layout_width/fill_parent').

回答1:


I have found a solution by means of changing values/attrs.xml to:

<?xml version="1.0" encoding="UTF-8"?>
<resources>    
    <attr name="layoutWidth" format="dimension">
        <enum name="fill_parent" value="-1" />
        <enum name="match_parent" value="-1" />
        <enum name="wrap_content" value="-2" />
    </attr>
</resources>

Now, I can write in values/themes.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme" parent="android:Theme"> 
        <item name="layoutWidth">match_parent</item>
    </style>
<resources>

But the question still remain: Is it possible to refer to Android layout_width parameter from this place?




回答2:


Well, this is a late response...but, for posterity and sanity, the answer can be found below and more info can be found at http://developer.android.com/guide/topics/ui/themes.html

styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="YourStyle" parent="@android:style/TextAppearance.Medium">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
    </style>
</resources>

This should work for any parent style that uses android:layout_width, such as parent="@android:style/Widget.TextView"




回答3:


You should create attrs.xml file inside values folder and then add the below code in it:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="custom_match_parent" type="dimen">-1</item>
    <item name="custom_wrap_content" type="dimen">-2</item>
</resources>

To access the above created attrs.xml add the below line inside your dimens.xml file as follows:

<dimen name="max_layout_width">@dimen/custom_match_parent</dimen>

Now to access that from a layout file you must do something like

<FrameLayout
    android:layout_width="@dimen/max_layout_width"
    android:layout_height="match_parent">
    <!--Do what you like to do here-->
</FrameLayout>

Hope this helps! :-)




回答4:


Not sure if you can add this attribute somewhere in your resources or style tags, but it may help (if its legal)

xmlns:android="http://schemas.android.com/apk/res/android"

This specifies the namespace where layout_width and layout_height are defined (as well as the constants fill_parent and wrap_content).



来源:https://stackoverflow.com/questions/8881651/how-to-refer-android-attribute-layout-width-from-custom-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!