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
404 views
in Technique[技术] by (71.8m points)

android - Value equals to match_parent or fill_parent in dimens.xml?

Based on here on XML Attributes section I specify following in my dimens.xml:

<dimen name="match_parent">-1dp</dimen>
<dimen name="main_left_menu_user_account_width">@dimen/match_parent</dimen>
<dimen name="main_left_menu_user_account_height">@dimen/match_parent</dimen>

Then I use the both dimensions in my layout:

<ImageView 
    android:id="@+id/userAccountImage"
    android:background="@drawable/user_account"
    android:layout_width="@dimen/main_left_menu_user_account_width"
    android:layout_height="@dimen/main_left_menu_user_account_height" />

Then, when I preview to Graphical Layout, it complains:

You must supply a layout_width attribute.

You must supply a layout_height attribute.

Actually can I define a value equals to match_parent in dimens.xml?

Update:

I also tried this but the preview still complains:

<dimen name="main_left_menu_user_account_width">-1dp</dimen>
<dimen name="main_left_menu_user_account_height">-1dp</dimen>

I successfully use wrap_content (the Graphical Layout doesn't complain at all):

<dimen name="wrap_content">-2dp</dimen>

<dimen name="main_right_menu_width">@dimen/wrap_content</dimen>
<dimen name="main_right_menu_height">@dimen/wrap_content</dimen>
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Use this, it works for me

<dimen name="custom_wrap_content">-2px</dimen>  
<dimen name="horizontal_border_height">@dimen /custom_wrap_content</dimen>

<dimen name="custom_match_parent">-1px</dimen>  
<dimen name="vertical_border_height">@dimen /custom_match_parent</dimen>

And the Reason why match_parent doesn't run. You cannot supply a build in keyword like match_parent

Edit: Use px instead of dp as suggested by Jarett Millard in the comments.


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

...