我想在布局的角落贴上徽章。我设法在布局中获得徽章,但无法在角落处实现。
目前我的代码给了我:
我想要的是什么:我希望将此徽章放在布局的右上角。
我想要这样的事情:
Toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="@dimen/toolbar_height"
android:background="@color/toolbar_color"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:text="My Assignments"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginLeft="90dp"
android:layout_weight="0.5"
android:textSize="20sp"
/>
<RelativeLayout
android:id="@+id/bell_linearlayout1"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="center"
android:layout_marginLeft="15dp">
<RelativeLayout
android:id="@+id/bell_linearlayout"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="#75aadb">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_centerInParent="true"
android:id="@+id/cartIconImageView"
android:src="@drawable/notification"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="25dip"
android:layout_height="25dip"
android:gravity="end|top|right"
android:layout_alignParentTop="true"
android:layout_marginBottom="100dp"
android:layout_marginLeft="20dp">
<TextView
android:id="@+id/textView1"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_alignParentTop="true"
android:background="@drawable/badge" />
</RelativeLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="35dp"
android:layout_height="35dp"
android:background="#75aadb"
android:id="@+id/accountinfo_layout"
android:layout_gravity="center"
android:layout_marginLeft="10dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:src="@drawable/profile"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.Toolbar>
Drawable badge.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="8dp" />
<solid android:color="#f20000" />
<stroke
android:width="2dip"
android:color="#FFF" />
<padding
android:bottom="6dp"
android:left="6dp"
android:right="6dp"
android:top="6dp" />
</shape>
请帮忙。
答案 0 :(得分:2)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layoutforprofileimage"
android:layout_width="100dp"
android:layout_height="100dp">
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:padding="2dp"
android:src="@drawable/image"
android:layout_height="100dp"
android:layout_margin="6dp"/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
android:layout_marginRight="0dp"
android:padding="2dp"
android:background="@drawable/button_pressed"
android:gravity="center"
android:minWidth="17sp"
android:adjustViewBounds="true"
android:minHeight="17sp"
android:paddingBottom="1dp"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:text="0"
android:textColor="#ffffffff"
android:textSize="12sp"
android:visibility="visible"
android:layout_gravity="center_horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
/>
</RelativeLayout>
答案 1 :(得分:0)
可以使用库设置徽章github.com/jgilfelt/android-viewbadger下载并添加到您的应用程序中。
并使用以下代码。
BadgeView homeimageviewbadge = new BadgeView(this,here add your layout);
homeimageviewbadge.setTextSize(12);
homeimageviewbadge.setTextColor(Color.parseColor("#ffffff"));
homeimageviewbadge.setBadgeBackgroundColor(Color.parseColor("#ff0000"));
homeimageviewbadge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
homeimageviewbadge.settext("10");
homeimageviewbadge.show();
答案 2 :(得分:0)
使用FrameLayout(而不是RelativeLayout)并将按钮和图像放入其中。
通过
定位图像(带数字的圈数)和按钮android:layout_gravity="top|left"
android:layout_marginTop="Xdp"
android:layout_marginLeft="Xdp"
希望这会对你有所帮助
答案 3 :(得分:0)
尝试这种方式获取BadgeCounter
public class BadgeDrawable extends Drawable {
private float mTextSize;
private Paint mBadgePaint;
private Paint mBadgePaint1;
private Paint mTextPaint;
private Rect mTxtRect = new Rect();
private String mCount = "";
private boolean mWillDraw = false;
public BadgeDrawable(Context context) {
mTextSize = context.getResources().getDimension(R.dimen.badge_text_size);
mBadgePaint = new Paint();
mBadgePaint.setColor(Color.RED);
mBadgePaint.setAntiAlias(true);
mBadgePaint.setStyle(Paint.Style.FILL);
mBadgePaint1 = new Paint();
mBadgePaint1.setColor(Color.parseColor("#EEEEEE"));
mBadgePaint1.setAntiAlias(true);
mBadgePaint1.setStyle(Paint.Style.FILL);
mTextPaint = new Paint();
mTextPaint.setColor(Color.WHITE);
mTextPaint.setTypeface(Typeface.DEFAULT);
mTextPaint.setTextSize(mTextSize);
mTextPaint.setAntiAlias(true);
mTextPaint.setTextAlign(Paint.Align.CENTER);
}
@Override
public void draw(Canvas canvas) {
if (!mWillDraw) {
return;
}
Rect bounds = getBounds();
float width = bounds.right - bounds.left;
float height = bounds.bottom - bounds.top;
// Position the badge in the top-right quadrant of the icon.
/*Using Math.max rather than Math.min */
float radius = ((Math.max(width, height) / 2)) / 2;
float centerX = (width - radius - 1) +10;
float centerY = radius -5;
if(mCount.length() <= 2){
// Draw badge circle.
canvas.drawCircle(centerX, centerY, radius+9, mBadgePaint1);
canvas.drawCircle(centerX, centerY, radius+7, mBadgePaint);
}
else{
canvas.drawCircle(centerX, centerY, radius+10, mBadgePaint1);
canvas.drawCircle(centerX, centerY, radius+8, mBadgePaint);
}
// Draw badge count text inside the circle.
mTextPaint.getTextBounds(mCount, 0, mCount.length(), mTxtRect);
float textHeight = mTxtRect.bottom - mTxtRect.top;
float textY = centerY + (textHeight / 2f);
if(mCount.length() > 2)
canvas.drawText("99+", centerX, textY, mTextPaint);
else
canvas.drawText(mCount, centerX, textY, mTextPaint);
}
/*
Sets the count (i.e notifications) to display.
*/
public void setCount(String count) {
mCount = count;
// Only draw a badge if there are notifications.
mWillDraw = !count.equalsIgnoreCase("0");
invalidateSelf();
}
@Override
public void setAlpha(int alpha) {
// do nothing
}
@Override
public void setColorFilter(ColorFilter cf) {
// do nothing
}
@Override
public int getOpacity() {
return PixelFormat.UNKNOWN;
}
}
或者如果您想在应用图标上使用徽章,请尝试使用ShortcutBadger
答案 4 :(得分:0)
也许有点旧,但如果有人读了这篇文章,现在你有了“新”ConstraintLayout的解决方案。
首先,我更喜欢在新的布局文件(文件名:user_cart_with_badge.xml)中执行此操作,我将其包含在工具栏中。
所以,在这个文件中,我将定义我想要使用它的图标,以及将用作徽章的文本。
此档案的内容:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layoutforprofileimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/btnOpenCart"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="?android:attr/selectableItemBackground"
android:gravity="end"
app:srcCompat="@drawable/ic_menu_shooping_cart"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="16dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:gravity="center"
android:minHeight="17sp"
android:minWidth="17sp"
android:padding="3dp"
android:paddingBottom="1dp"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:scaleType="fitStart"
android:text="3"
android:textColor="#ffffff"
android:textSize="12sp"
android:visibility="visible"
android:background="@drawable/badge_background"
app:layout_constraintBottom_toBottomOf="@+id/btnOpenCart"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/btnOpenCart"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
现在,我们还应该为TextView徽章定义背景。在drawable文件夹中创建新文件,我称之为“badge_background.xml”。我认为在这个link上你可以看到更多关于这个背景的形状,如果你不喜欢我的。
此档案的内容:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners android:radius="2dp" />
<solid
android:color="#ff4848"/>
<padding
android:bottom="6dp"
android:left="6dp"
android:right="6dp"
android:top="6dp" />
</shape>
最后,您现在需要的是将user_cart_with_badge.xml布局的内容包含到文件/布局中,您需要带有徽章的自定义图像。假设您在工具栏中需要它。
<android.support.v7.widget.Toolbar
android:id="@+id/toolbarMeat"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="@style/AppTheme.Toolbar"
android:gravity="end">
<include
android:id="@+id/cardContainerForSettingsData"
layout="@layout/user_cart_with_badge"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/user_info_section" />
</android.support.v7.widget.Toolbar>
希望,这可以帮助那些读过这本书的人,多年后。
答案 5 :(得分:0)
我发布此消息可能很晚,但是我对此一无所知,并找到了一个非常简单的解决方案,但是使用align right和align top是我发现的最简单的方法。希望这对某些人有帮助。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/opened"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/opened"
android:layout_centerInParent="true"
android:layout_marginTop="@dimen/_10sdp"
/>
<TextView
android:id="@+id/notsopened"
android:layout_alignRight="@+id/opened"
android:layout_alignTop="@+id/opened"
android:background="@drawable/notification_iconmain"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>