Android 세트 네비게이션 서랍 목록 모든 장치 화면에 대한 화면의 정확한 절반을 엽니 다.
-
21-12-2019 - |
문제
모든 다른 장치에 대해 화면의 절반으로 백을 열고 싶습니다.Layout_MargineLeft 200dp 또는 100dp 용 하드 코딩 된 값을 인출 목록에 시도했습니다.그러나 장치에서 장치까지 모든 장치에서 작동하지 않습니다.그렇다면 서랍리스트의 화면의 정확히 절반을 어떻게 유지합니까?나는 또한 setleft (int)와 같은 다양한 기능을 시도했다. 그러나 일부는 최소 버전 8을 사용하기 때문에 일부가 작동하지 않습니다. 그래서 제발 도와주세요.미리 감사드립니다.
mDrawerLayout = (DrawerLayout) view.findViewById(R.id.drawer_layout);
mDrawerList = (ListView) view.findViewById(R.id.top_sectionlist);
.
XML :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/setting_background" >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_margin="@dimen/top_news_linear_margin"
android:background="@color/news_list_divider_color"
android:orientation="vertical"
android:padding="@dimen/top_news_linear_padding" >
</RelativeLayout>
<ListView
android:id="@+id/drawer_list"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="right"
android:layout_alignParentTop="true"
android:background="@color/setting_background"
android:cacheColorHint="@android:color/transparent"
android:choiceMode="singleChoice"
android:divider="@color/news_list_divider_color"
android:dividerHeight="@dimen/news_list_divider_height"
/>
</android.support.v4.widget.DrawerLayout>
. 해결책
ListView의 너비를 동적으로 설정 ...
mDrawerLayout = (DrawerLayout) view.findViewById(R.id.drawer_layout);
mDrawerList = (ListView) view.findViewById(R.id.top_sectionlist);
int width = getResources().getDisplayMetrics().widthPixels/2;
DrawerLayout.LayoutParams params = (android.support.v4.widget.DrawerLayout.LayoutParams) mDrawerList.getLayoutParams();
params.width = width;
mDrawerList.setLayoutParams(params);
. 다른 팁
수동으로 화면 크기를 계산하고 런타임에 서랍 레이아웃에 동적 너비를 설정해야합니다. 여기에는 런타임시 장치 화면 너비와 높이를 가져 오는 방법
이 코드를 사용하면 런타임 디스플레이의 너비와 높이를 얻을 수 있습니다
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
.
이제 폭을 2
로 나누어야합니다.int newWidth=width/2;
.
이제 wide to listview를 다음과 같이 설정합니다
drawer_list.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,newWidth));
.
모든 장치에서 작동하며 모든 것에만큼 균일 한 크기를 제공합니다.
here is my solution for others who need several percentage to show there navigation drawer.
Here, I use right navigation drawer & i show 15% of home screen when drawer move left.
// convert your offset percent (here 15% ) into decimal by dividing 100
float offset = .15f * getResources().getDisplayMetrics().widthPixels ;
float width = getResources().getDisplayMetrics().widthPixels - offset;
DrawerLayout.LayoutParams params = (android.support.v4.widget.DrawerLayout.LayoutParams) rightNavigationView.getLayoutParams();
params.width = (int) width;
rightNavigationView.setLayoutParams(params);
Using the new Android Support Design Library, you can make use of the android.support.percent.PercentRelativeLayout
to accomplish this easily. Here's my solution.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="-64dp"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="64dp"/>
<android.support.percent.PercentRelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:fitsSystemWindows="true"
app:layout_widthPercent="50%"
app:headerLayout="@layout/nav_header_dashboard"
app:menu="@menu/menu_drawer_activity_dashboard"/>
</android.support.percent.PercentRelativeLayout>
</android.support.v4.widget.DrawerLayout>
If you're asking why there's a android:layout_marginRight="-64dp"
and android:layout_marginRight="-64dp"
in the DrawerLayout
and the content. It's because of the fixed margin of 64 dp that is coded directly to the DrawerLayout
that wouldn't allow you to use a full widht of the drawer. More about that here
int currentVerion = Build.VERSION.SDK_INT;
int width= 0;
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
if(currentVerion >= Build.VERSION_CODES.BASE) {
display.getSize(size);
width = size.x;
}
else
{
width = display.getWidth();
}
((ListView) view.findViewById(R.id.top_sectionlist)).getLayoutParams().width =width/2;