- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试做抽屉导航选项。
这是我的 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/sendnewmessages"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:background="@drawable/nothere"
android:gravity="center_horizontal"
android:shadowColor="@android:color/black"
android:shadowDx="1"
android:shadowDy="1"
android:shadowRadius="1"
android:text="Send New Messages"
android:textAlignment="center"
android:textColor="@android:color/black" />
<EditText
android:id="@+id/searchmessage"
android:layout_height="40dp"
android:layout_width="200dp"
android:gravity="center"
android:hint="Search Messages"/>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
如果我删除 Linearlayout
则没有错误。此按钮出现在 listview
后面,edittext
出现在屏幕中间。问题是对象没有对齐。有没有办法在 DrawerLayout
中对齐 View 对象?
如果我不删除 linearlayout
我会得到这个错误:
LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
堆栈跟踪:
E/AndroidRuntime(3454): FATAL EXCEPTION: main Process: com.brilliant.bridge, PID: 3454 java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams at android.support.v4.widget.DrawerLayout.isDrawerView(DrawerLayout.java:910) at android.support.v4.widget.DrawerLayout.isDrawerOpen(DrawerLayout.java:1156) at com.brilliant.bridge.AdminMessages.onPrepareOptionsMenu(AdminMessages.java:220) at android.app.Activity.onPreparePanel(Activity.java:2556) at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:464) at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800) at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) at android.view.Choreographer.doCallbacks(Choreographer.java:574) at android.view.Choreographer.doFrame(Choreographer.java:543) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method)
public boolean onPrepareOptionsMenu(Menu menu) {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
最佳答案
在 xml 文件中为您的 LinearLayout 分配一个 ID:
<LinearLayout
android:id="@+id/DrawerLinear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- YOUR LISTVIEW IS HERE -->
</LinearLayout>
然后在你的 java 类中,转换这个 LinearLayout:
private LinearLayout DrawerLinear;
在 OnCreate 中你应该这样做:
DrawerLinear = (LinearLayout) findViewById(R.id.DrawerLinear);
在你的 onPrepareOptionMenu 中,把这个:
public boolean onPrepareOptionsMenu(Menu menu) {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(DrawerLinear);
menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
它应该可以解决您的问题。
关于android - LinearLayout$LayoutParams 无法转换为 android.support.v4.widget.DrawerLayout$LayoutParams,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23955603/
我试图在 2 个小部件/布局之间创建一个 float 操作按钮 我 99% 确定我已经使用了这个教程并且有效 How can I add the new "Floating Action Button
这是 XML 代码,
因为它可以与Relative 或Linear 布局一起使用。因此,如果我们稍后将 RelativeLayout 更改为 LinearLayout,它可能会很有用。 LinearLayout.Layou
我正在尝试开发 Android 3.1 平板电脑应用程序。 这个app会有很多图片,我已经关注了Processing Bitmaps Off the UI Thread教程,但我做错了,因为我得到:
我的初始代码行位于重写方法 onGlobalLayout() 中: img.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 10
我正尝试按照发布在以下位置的示例编写周历: How can I create a weekly calendar view for an Android Honeycomb application?
这段代码似乎适用于 android api 16 - 22,但不适用于 api 23。我只是想显示一个带有选项的弹出窗口,并使弹出窗口下方的背景变暗: WindowPo
带有 Android Studio 2.0 的 OS X Yosemite 10.10.5 出于教学目的,我想将尽可能多的 View 属性从 XML 移动到 Java。 我在 GridLayout 中
说明及解决方法在底部。 我正在开发一个 slider 布局动画,动画效果很好,但是当所有进程结束时,它们会得到下一个异常。 我猜 RelativeLayout 父级在异常中有一些事情要做,但我不知道如
我正在尝试创建一个 FloatingActionButton,如 this , 但是当我运行应用程序时出现以下错误 java.lang.ClassCastException: android.wid
根据大量教程和示例项目,我得到了以下代码,它应该用包含来自 Line 对象的多个值的行填充 ListView。 作为一名新手程序员,我一辈子都弄不明白我的代码到底哪里出错了。任何帮助将不胜感激! 如果
我得到了这个异常(exception): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cas
Android Studio 3.1、Java 1.8、Gradle 4.1。我使用 minVersion = 15 我想在文件 profile_category_active.xml 中以编程方式设
我正在尝试使我的整个布局(其中包含 ListView)可滚动。也就是说,整个布局应该拥有滚动条,而不仅仅是 ListView。我正在尝试使用 commonsguy 的 MergeAdapter ( h
我遇到了以下错误 java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to an
还有其他相关问题,但它们没有解决我的情况(它们的错误代码与客户端代码的某种回收和/或错误的转换调用有关)。 我的情况比较复杂。 我有一些代码,用户可以在其中调出照片库。 问题是,它在我拥有的 7 部手
我正在尝试做抽屉导航选项。 这是我的 xml: 如果我删除 Linearlayout
我正在尝试获取 AppBarLayout 的布局参数,但出现此错误。 java.lang.ClassCastException: android.support.design.widget.Coord
我使用一个具有RelativeLayout -> ScrollView -> LinearLayout 的 View 。我得到了异常(exception): 11-22 11:17:24.605: E
我在调试我正在处理的 Android 应用程序时遇到了问题。目前,它应该能够从文本文件中获取输入(它可以),将其转换为包含表示网格部分的 Cell 对象的 ArrayList(它确实如此),然后使用
我是一名优秀的程序员,十分优秀!