gpt4 book ai didi

android - 将 View 添加到抽屉导航中的 ListView

转载 作者:行者123 更新时间:2023-11-29 00:13:08 26 4
gpt4 key购买 nike

因此,我尝试将抽屉导航添加到我的应用程序中,因此作为开始,我使用 android 抽屉导航示例创建了一个示例项目。

据我所知,他们使用列表适配器将项目添加到抽屉导航。

我想再添加一些东西:

  • 标题中的 TextView 。

  • 一个按钮

  • 按类别分隔的一堆项目,例如:事件、消息等...

我尝试使用 addView 方法,但应用程序崩溃了。

然后我使用了 addHeaderView,它在标题中添加了 TextView ,但这还不够......

将 View 添加到 ListView 的正确方法是什么?

最佳答案

这就是解决方案

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 做一些事情。*

enter image description here

关于android - 将 View 添加到抽屉导航中的 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28792643/

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