【问题标题】:Set LayoutParams of ListView when using DrawerLayout with Fragments使用带有 Fragments 的 DrawerLayout 时设置 ListView 的 LayoutParams
【发布时间】:2014-04-30 18:32:07
【问题描述】:

我想更改位于我的DrawerLayout 中的Listview 的宽度和高度:

DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
DrawerLayout.LayoutParams myParam = new DrawerLayout.LayoutParams(20, 20);
mDrawerList.setLayoutParams(myParam);

但我收到此错误:

06-21 09:30:57.361: E/AndroidRuntime(12583): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jk.demo/com.jk.demo.MemoStart}:
  java.lang.IllegalArgumentException: View android.widget.ListView{414506a8 VFED.VC. ......I. 0,0-0,0 #7f04003a app:id/left_drawer} is not a sliding drawer

drawer_main.xml

<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" >

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="540dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#111"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp" />

我导入了:

import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.LayoutParams;

有什么解决办法吗?

【问题讨论】:

  • 即使使用 ListView.LayoutParams 我也遇到了同样的问题:ListView.LayoutParams myParam = new ListView.LayoutParams(20, 20);这是我的错误:06-30 20:57:30.431: E/AndroidRuntime(18212):Caused by: java.lang.IllegalArgumentException:View android.widget.ListView{4271e6f0 V.ED.VC. ......I. 0,0-0,0 #7f040035 app:id/left_drawer} is not a sliding drawer

标签: android listview layoutparams drawerlayout


【解决方案1】:

无需创建新的 layoutParam 对象,您可以像这样设置列表视图的宽度/高度

mDrawerList.getLayoutParams().width = 20;
mDrawerList.getLayoutParams().height = 20;

请注意,20 以像素为单位。如果你想(通常)设置 dp 值,那么你需要做一些转换

int widthDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics());
int heightDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics());
mDrawerList.getLayoutParams().width = widthDp;
mDrawerList.getLayoutParams().height = heightDp;

【讨论】:

    【解决方案2】:

    如果您为列表视图使用自定义适配器,您可以在最后一项中添加合适的图片。
    为了不错过最后一项,您可以将无效数据添加到列表数据中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-28
      • 1970-01-01
      • 2015-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-30
      • 1970-01-01
      相关资源
      最近更新 更多