How to specify fixed layout_marginLeft for smartphones and wrap_content for tablet for NavigationDrawer

白昼怎懂夜的黑 提交于 2019-12-25 19:06:50

问题


I have the below navigation drawer and I want to give layout_marginLeft as -64dp for smartphones and wrap_content for tablets. Please let me know how to do that?I don't want to create separate laytout.xml files for smartphone and tablets i.e I have only one layout.xml file. As of now I am specifying different dimension in dimes.xml files which are created for smartphones and tabs.

<!-- The navigation drawer -->
<android.support.design.widget.NavigationView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:layout_marginLeft="-64dp"

回答1:


Assuming that you know how to create different dimes.xml files for multiple screens (If you don't know that have a look in How to define dimens.xml for every different screen size in android?) your problem should be how to set android:layout_marginLeft in the specific xml file using dimen value.

You can try this way

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

for match_parent> <dimen name="your_value">-1dp</dimen>

and set android:layout_marginLeft="@dimen/your_value"




回答2:


You Just have to create 5 layouts file for all screen sizes with same name(of layout file) and store it into different folder In (hdpi,mdpi,xhdpi,xxhdpi,xxxhdpi).



来源:https://stackoverflow.com/questions/42487810/how-to-specify-fixed-layout-marginleft-for-smartphones-and-wrap-content-for-tabl

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