将视图添加到导航抽屉中的列表视图
add views to list view in navigation drawer
所以我尝试将导航抽屉添加到我的应用程序中,因此作为开始,我使用 android 导航抽屉示例创建了一个示例项目。
据我所知,他们使用列表适配器将项目添加到导航抽屉。
我想再补充几句:
header中的文本视图。
一个按钮
一堆按类别分隔的项目,例如:事件、消息等...
我尝试使用 addView 方法,但应用程序崩溃了。
然后我使用 addHeaderView 在 header 中添加了文本视图,但这还不够...
将视图添加到列表视图的正确方法是什么?
如果您正在使用 Navigation Drawer,最好使用 Fragments。
这是我几天前创建的导航抽屉的 Github link。它的代码很容易理解。如果您发现任何问题,您可以回来。
如果你想了解它的工作原理,你可以得到 documentation here.
所以这是解决方案
android.support.v4.widget.DrawerLayout 是 Android 历史上一个了不起的 UI 元素。假设您在 android.support.v4.widget.DrawerLayout 中有两个 ChildView。那么第一个 Child 是左抽屉,下一个 CHILD 是右抽屉
这里有一个示例,仔细阅读代码,理解结构。
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.rh.bookmany"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!--What ever your want in the Right drawer, put it in below RelativeLayout -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/white" >
<!-- TO SHOW FRAGMENTS -->
<FrameLayout
android:id="@+id/mFragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<!--What ever your want in the LEFT drawer, put it in below RelativeLayout -->
<!-- LEFT DRAWER -->
<RelativeLayout
android:id="@+id/whatYouWantInLeftDrawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/black" >
<!-- Cover or Banner -->
<RelativeLayout
android:id="@+id/rlBanner"
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical" >
<!-- Cover Pic Container -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp"
>
<!-- Cover Picture -->
<ImageView
android:id="@+id/ivCoverPic"
android:layout_width="match_parent"
android:layout_height="150dp"
android:contentDescription="@string/dummy_desc"
android:scaleType="centerCrop"
android:src="@drawable/skrillex" />
<!-- Tint -->
<LinearLayout
android:orientation="vertical"
android:id="@+id/llCoverTint"
android:layout_height="150dp"
android:layout_width="match_parent"
></LinearLayout>
</RelativeLayout>
<!-- Profile Pic Container -->
<LinearLayout
android:id="@+id/llProfilePicContainer"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="7dp"
android:orientation="vertical" >
<com.rh.bookmany.view.MLRoundedImageView
android:id="@+id/civProfilePic"
android:src="@drawable/tony"
android:layout_height="65dp"
android:layout_width="65dp"
/>
</LinearLayout>
<TextView
android:id="@+id/tvUserEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvUserName"
android:layout_below="@+id/tvUserName"
android:textColor="@color/white"
android:textSize="12sp" />
<TextView
android:id="@+id/tvUserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/llProfilePicContainer"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/llProfilePicContainer"
android:textColor="@color/white"
android:textSize="15sp" />
</RelativeLayout>
<!-- ListMenu -->
<ListView
android:id="@+id/navigation_menu_container"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_below="@id/rlBanner"
android:layout_gravity="start"
android:background="@color/nyc_black"
android:choiceMode="singleChoice"
android:divider="@color/border_black"
android:dividerHeight="@dimen/divider_height"
android:listSelector="@drawable/item_selector" >
</ListView>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
MLRoundedImageView.java
package your.package.name;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
public class MLRoundedImageView extends ImageView {
public MLRoundedImageView(Context context) {
super(context);
}
public MLRoundedImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MLRoundedImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
Log.d("X","MLRoundedImageView ONDRAW");
Drawable drawable = getDrawable();
if (drawable == null) {
return;
}
if (getWidth() == 0 || getHeight() == 0) {
return;
}
Bitmap b = ((BitmapDrawable) drawable).getBitmap();
Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
int w = getWidth(), h = getHeight();
Bitmap roundBitmap = getCroppedBitmap(bitmap, w);
canvas.drawBitmap(roundBitmap, 0, 0, null);
}
public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
Bitmap sbmp;
if (bmp.getWidth() != radius || bmp.getHeight() != radius) {
float smallest = Math.min(bmp.getWidth(), bmp.getHeight());
float factor = smallest / radius;
sbmp = Bitmap.createScaledBitmap(bmp, (int)(bmp.getWidth() / factor), (int)(bmp.getHeight() / factor), false);
} else {
sbmp = bmp;
}
Bitmap output = Bitmap.createBitmap(radius, radius,
Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xffa19774;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, radius, radius);
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.parseColor("#BAB399"));
canvas.drawCircle(radius / 2 + 0.7f,
radius / 2 + 0.7f, radius / 2 + 0.1f, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(sbmp, rect, rect, paint);
return output;
}
}
以上代码可以产生如下输出。
- 请注意,只有 XML,你不能做这样的事情。尽管您必须在 Activity 中使用 JAVA 做一些事情。*
所以我尝试将导航抽屉添加到我的应用程序中,因此作为开始,我使用 android 导航抽屉示例创建了一个示例项目。
据我所知,他们使用列表适配器将项目添加到导航抽屉。
我想再补充几句:
header中的文本视图。
一个按钮
一堆按类别分隔的项目,例如:事件、消息等...
我尝试使用 addView 方法,但应用程序崩溃了。
然后我使用 addHeaderView 在 header 中添加了文本视图,但这还不够...
将视图添加到列表视图的正确方法是什么?
如果您正在使用 Navigation Drawer,最好使用 Fragments。
这是我几天前创建的导航抽屉的 Github link。它的代码很容易理解。如果您发现任何问题,您可以回来。
如果你想了解它的工作原理,你可以得到 documentation here.
所以这是解决方案
android.support.v4.widget.DrawerLayout 是 Android 历史上一个了不起的 UI 元素。假设您在 android.support.v4.widget.DrawerLayout 中有两个 ChildView。那么第一个 Child 是左抽屉,下一个 CHILD 是右抽屉
这里有一个示例,仔细阅读代码,理解结构。
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.rh.bookmany"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!--What ever your want in the Right drawer, put it in below RelativeLayout -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/white" >
<!-- TO SHOW FRAGMENTS -->
<FrameLayout
android:id="@+id/mFragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<!--What ever your want in the LEFT drawer, put it in below RelativeLayout -->
<!-- LEFT DRAWER -->
<RelativeLayout
android:id="@+id/whatYouWantInLeftDrawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/black" >
<!-- Cover or Banner -->
<RelativeLayout
android:id="@+id/rlBanner"
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical" >
<!-- Cover Pic Container -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp"
>
<!-- Cover Picture -->
<ImageView
android:id="@+id/ivCoverPic"
android:layout_width="match_parent"
android:layout_height="150dp"
android:contentDescription="@string/dummy_desc"
android:scaleType="centerCrop"
android:src="@drawable/skrillex" />
<!-- Tint -->
<LinearLayout
android:orientation="vertical"
android:id="@+id/llCoverTint"
android:layout_height="150dp"
android:layout_width="match_parent"
></LinearLayout>
</RelativeLayout>
<!-- Profile Pic Container -->
<LinearLayout
android:id="@+id/llProfilePicContainer"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="7dp"
android:orientation="vertical" >
<com.rh.bookmany.view.MLRoundedImageView
android:id="@+id/civProfilePic"
android:src="@drawable/tony"
android:layout_height="65dp"
android:layout_width="65dp"
/>
</LinearLayout>
<TextView
android:id="@+id/tvUserEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvUserName"
android:layout_below="@+id/tvUserName"
android:textColor="@color/white"
android:textSize="12sp" />
<TextView
android:id="@+id/tvUserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/llProfilePicContainer"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/llProfilePicContainer"
android:textColor="@color/white"
android:textSize="15sp" />
</RelativeLayout>
<!-- ListMenu -->
<ListView
android:id="@+id/navigation_menu_container"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_below="@id/rlBanner"
android:layout_gravity="start"
android:background="@color/nyc_black"
android:choiceMode="singleChoice"
android:divider="@color/border_black"
android:dividerHeight="@dimen/divider_height"
android:listSelector="@drawable/item_selector" >
</ListView>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
MLRoundedImageView.java
package your.package.name;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
public class MLRoundedImageView extends ImageView {
public MLRoundedImageView(Context context) {
super(context);
}
public MLRoundedImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MLRoundedImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
Log.d("X","MLRoundedImageView ONDRAW");
Drawable drawable = getDrawable();
if (drawable == null) {
return;
}
if (getWidth() == 0 || getHeight() == 0) {
return;
}
Bitmap b = ((BitmapDrawable) drawable).getBitmap();
Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
int w = getWidth(), h = getHeight();
Bitmap roundBitmap = getCroppedBitmap(bitmap, w);
canvas.drawBitmap(roundBitmap, 0, 0, null);
}
public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
Bitmap sbmp;
if (bmp.getWidth() != radius || bmp.getHeight() != radius) {
float smallest = Math.min(bmp.getWidth(), bmp.getHeight());
float factor = smallest / radius;
sbmp = Bitmap.createScaledBitmap(bmp, (int)(bmp.getWidth() / factor), (int)(bmp.getHeight() / factor), false);
} else {
sbmp = bmp;
}
Bitmap output = Bitmap.createBitmap(radius, radius,
Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xffa19774;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, radius, radius);
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.parseColor("#BAB399"));
canvas.drawCircle(radius / 2 + 0.7f,
radius / 2 + 0.7f, radius / 2 + 0.1f, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(sbmp, rect, rect, paint);
return output;
}
}
以上代码可以产生如下输出。
- 请注意,只有 XML,你不能做这样的事情。尽管您必须在 Activity 中使用 JAVA 做一些事情。*