如何在 NavigationDrawer 的底部设置一个 ListView?
How to set a ListView at the bottom of a NavigationDrawer?
我正在开发一个应用程序,但遇到一个问题,我无法在 DrawerLayout 底部显示 ListView,这是我的代码:
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<android.webkit.WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView" />
</LinearLayout>
<ListView
android:id="@+id/navList"
android:layout_width="180dp"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:layout_gravity="left|start"
android:background="#ffffff" />
</android.support.v4.widget.DrawerLayout>
这是目前的样子:
我尝试在 RelativeLayout 中设置 ListView,但它根本不起作用(例如,我使用了当前答案之一,尽管我之前自己尝试过。)。这是发生了什么:
此外,我也尝试设置一个 ImageView 来创建一些 space,但在这两种情况下,代码都破坏了代码。
这就是我想要做的:
有人知道吗?谢谢
您应该添加一个 LinearLayout
作为具有 match_parent
宽度和高度的 DrawerLayout
的子项。然后把你的 ListView
放在那个 LinearLayout
的底部。像下面这样的东西。我不知道你到底想要怎样的布局,但下面的代码将列表视图放在 LinearLayout
的底部
<?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="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.9"
android:minHeight="25px"
android:minWidth="25px"
android:orientation="vertical">
<android.webkit.WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<ListView
android:id="@+id/navList"
android:layout_width="180dp"
android:layout_height="0dp"
android:layout_gravity="left|start"
android:layout_weight="0.1"
android:background="#ffffff"
android:choiceMode="singleChoice" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
为了使其正常工作,进行了多项更改:
先创建一个Fragmentclass:
public class BlankFragment : Fragment
{
public BlankFragment()
{
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout for this fragment
return inflater.Inflate(Resource.Layout.BlankFragment, container, false);
}
}
其次,将XML中的ListView替换为Fragment:
<fragment
android:name="your package name.BlankFragment"
android:layout_width="180dp"
android:layout_height="match_parent"
android:layout_gravity="left"/>
最后:
将 ListView 移动到片段 XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="180dp"
android:layout_height="match_parent"
android:background="#e00808"
tools:context=".BlankFragment">
<!-- TODO: Update blank fragment layout -->
<ListView
android:id="@+id/navList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice"
android:layout_alignParentBottom="true"
android:background="#ffffff" />
</RelativeLayout>
此外,您需要将 android:layout_height="match_parent"
更改为 android:layout_height="wrap_content"
。然后结果会低于预期。
我在 Xamarin 论坛上得到了 robbit 的大力支持:
你可以看看他的答案!谢谢robbit!
我正在开发一个应用程序,但遇到一个问题,我无法在 DrawerLayout 底部显示 ListView,这是我的代码:
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<android.webkit.WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView" />
</LinearLayout>
<ListView
android:id="@+id/navList"
android:layout_width="180dp"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:layout_gravity="left|start"
android:background="#ffffff" />
</android.support.v4.widget.DrawerLayout>
这是目前的样子:
我尝试在 RelativeLayout 中设置 ListView,但它根本不起作用(例如,我使用了当前答案之一,尽管我之前自己尝试过。)。这是发生了什么:
此外,我也尝试设置一个 ImageView 来创建一些 space,但在这两种情况下,代码都破坏了代码。
这就是我想要做的:
有人知道吗?谢谢
您应该添加一个 LinearLayout
作为具有 match_parent
宽度和高度的 DrawerLayout
的子项。然后把你的 ListView
放在那个 LinearLayout
的底部。像下面这样的东西。我不知道你到底想要怎样的布局,但下面的代码将列表视图放在 LinearLayout
<?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="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.9"
android:minHeight="25px"
android:minWidth="25px"
android:orientation="vertical">
<android.webkit.WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<ListView
android:id="@+id/navList"
android:layout_width="180dp"
android:layout_height="0dp"
android:layout_gravity="left|start"
android:layout_weight="0.1"
android:background="#ffffff"
android:choiceMode="singleChoice" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
为了使其正常工作,进行了多项更改:
先创建一个Fragmentclass:
public class BlankFragment : Fragment
{
public BlankFragment()
{
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout for this fragment
return inflater.Inflate(Resource.Layout.BlankFragment, container, false);
}
}
其次,将XML中的ListView替换为Fragment:
<fragment
android:name="your package name.BlankFragment"
android:layout_width="180dp"
android:layout_height="match_parent"
android:layout_gravity="left"/>
最后:
将 ListView 移动到片段 XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="180dp"
android:layout_height="match_parent"
android:background="#e00808"
tools:context=".BlankFragment">
<!-- TODO: Update blank fragment layout -->
<ListView
android:id="@+id/navList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice"
android:layout_alignParentBottom="true"
android:background="#ffffff" />
</RelativeLayout>
此外,您需要将 android:layout_height="match_parent"
更改为 android:layout_height="wrap_content"
。然后结果会低于预期。
我在 Xamarin 论坛上得到了 robbit 的大力支持:
你可以看看他的答案!谢谢robbit!