gpt4 book ai didi

android - 向抽屉导航添加线性布局(最终因 ClassCastException 而崩溃)

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:08:13 25 4
gpt4 key购买 nike

我正在尝试向抽屉导航添加垂直线性布局(具有标题、图像、菜单标题、 ListView 和底部的图标)。但是应用程序崩溃显示

'java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams'

错误。我不确定这是否是可能的直接方式,因为我检查了几个链接并且我得到的唯一关闭是 this.任何帮助表示赞赏。提前致谢。这是我的代码。

<!-- The main context view -->

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

<!-- The navigation drawer -->

<LinearLayout
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#fff"
android:gravity="bottom|center"
android:orientation="vertical"
android:paddingTop="30dp" >

<ListView
android:id="@+id/left_drawer"
android:choiceMode="singleChoice"
android:divider="#E8E8E8"
android:dividerHeight="1dp"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:src="@drawable/footer_image" />
</LinearLayout>

现在的问题是,xml 不允许我为 ListView 定义宽度和高度(元素未知)而且我也无法运行它,因为编译器说“您必须提供 layout_width”属性?请帮忙。

我的 ScrollView 代码。

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

<!-- The main context view -->

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

<!-- The navigation drawer -->

<ScrollView
android:id="@+id/leftRL"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginBottom="5dp"
android:background="#fff" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="3dp"
android:src="@drawable/ic_launcher"
android:text="Dummy"
android:textColor="#2E3192"
android:textStyle="bold" />

<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#2E3192" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="2dp" >

<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="#e8e8e8"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" />

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp" >

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dummy name"
android:textSize="15sp" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dummyemail@imhot.com"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:paddingBottom="3dp"
android:src="@drawable/ic_launcher"
android:text="Dummy item"
android:textColor="#2E3192"
android:textStyle="bold" />

<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#2E3192" />
</LinearLayout>

<ListView/>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="40dp"
android:padding="10dp"
android:src="@drawable/footer_image" />
</LinearLayout>
</ScrollView>

</android.support.v4.widget.DrawerLayout>

最佳答案

如果没有代码和堆栈跟踪,您的查询很难理解,但无论如何..

记住 android.support.v4.widget.DrawerLayout 只能有 3 个元素(完全按照这个顺序):

  1. 你的主页

  2. 左抽屉

  3. 右抽屉

复制粘贴此示例(只有前两个元素)并继续您的要求

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

<!-- Your main screen -->

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<RelativeLayout
android:id="@+id/topRL"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#DE6D53"
android:orientation="horizontal" >

<ImageButton
android:id="@+id/left_drawer_button"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:onClick="onOpenLeftDrawer"
android:src="@drawable/ic_launcher" />

</RelativeLayout>

<FrameLayout
android:id="@+id/frame_to_be_replaced"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/topRL" />
</RelativeLayout>

<!-- left drawer -->

<RelativeLayout
android:id="@+id/whatYouWantInLeftDrawer"
android:layout_width="290dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/black" >

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="title"
android:textColor="@android:color/white" />

<ListView
android:id="@+id/left_expandableListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start" />

<TextView
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentBottom="true"
android:textColor="@android:color/white"
android:text="bottom" />
</RelativeLayout>

</android.support.v4.widget.DrawerLayout>

您的 Activity :

public class MainActivity extends Activity {
RelativeLayout leftRL;
RelativeLayout rightRL;
DrawerLayout drawerLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// I'm removing the ActionBar.
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.temp);

leftRL = (RelativeLayout)findViewById(R.id.whatYouWantInLeftDrawer);
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
}

public void onOpenLeftDrawer(View view)
{
drawerLayout.openDrawer(leftRL);
}
}

关于android - 向抽屉导航添加线性布局(最终因 ClassCastException 而崩溃),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21621673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com