Tôi đã sử dụng mẫu Ngăn điều hướng có trong Android Studio. Trong bố cục hoạt động của tôi, tôi có mã sau đây:Android thay đổi chiều rộng của bảng ngăn điều hướng theo chương trình
<fragment android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.smedilepaolo.newsfeed.NavigationDrawer.NavigationDrawerFragment"
tools:layout="@layout/fragment_navigation_drawer" />
Điều tôi cần là thay đổi độ rộng của bảng điều khiển theo chương trình. Tôi nghĩ rằng khu vực thích hợp để thay đổi nó là:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_feed);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
....
Nhưng tôi không thể hiểu làm thế nào để đạt được mục tiêu của mình.
/* CẬP NHẬT */
Biểu tượng của bảng điều khiển ngăn không cho thấy trong thanh hành động
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
mDrawerListView.post(new Runnable() {
@Override
public void run() {
Resources resources = getResources();
float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 240, resources.getDisplayMetrics());
DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) mDrawerListView.getLayoutParams();
params.width = (int) (width);
mDrawerListView.setLayoutParams(params);
}
});
selectItem(position);
}
});
mDrawerListView.post(new Runnable() {
@Override
public void run() {
Resources resources = getResources();
float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, resources.getDisplayMetrics().widthPixels-1, resources.getDisplayMetrics());
DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) mDrawerListView.getLayoutParams();
params.width = (int) (width);
mDrawerListView.setLayoutParams(params);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
mDrawerLayout.openDrawer(mFragmentContainerView);
}
});
Đó là phương pháp selectItem
private void selectItem(int position) {
mCurrentSelectedPosition = position;
if (mDrawerListView != null) {
mDrawerListView.setItemChecked(position, true);
}
if (mDrawerLayout != null) {
mDrawerLayout.closeDrawer(mFragmentContainerView);
}
....
Cảm ơn bạn rất nhiều! nó hoạt động! Tôi sử dụng mã của bạn để tự động đặt chiều rộng thành khả năng tối đa. Sau lần nhấp đầu tiên trên một mục, tôi sử dụng lại mã của bạn để thay đổi kích thước chiều rộng thành 240dp. Nhưng tiếc là nó loại bỏ các biểu tượng của trình đơn ngăn kéo ở góc trên cùng của thanh bên ... Tôi không biết tại sao ... Làm thế nào tôi có thể sửa chữa? Nó cũng có thể tự động mở menu ngăn kéo và vô hiệu hóa cử chỉ pan (và reable sau khi nhấp chuột đầu tiên vào một mục) –
Tôi sửa vấn đề biểu tượng bằng cách sử dụng resources.getDisplayMetrics(). WidthPixels-1 thay vì resources.getDisplayMetrics(). widthPixels! vì vậy các câu hỏi còn lại của tôi bây giờ là 2. Có thể mở bảng ngăn kéo theo chương trình và tắt cử chỉ pan không? Cảm ơn :) –
Bạn được hoan nghênh! Xem câu trả lời chỉnh sửa của tôi :) –