我是开发人员。我需要实现如下所示的设计。我已经有功能性的应用程序,但想知道如何解决这个问题?特别是,我对如何在标签下显示“新”项的数量感兴趣。我知道该怎么做-用红点创建新图标,并在有新内容时显示它们。
但是我不知道如何使这些圆形圆圈漂浮在标题上方并在其中显示数字。有人对寻找的东西也有建议吗?样品?方向?
关于分离活动的第二个问题。我是否应该进行控制以合并这样的按钮,然后在活动中进行充气?否则,我可能会创建选项卡式活动,但不确定是否可以设置其样式以使其看起来像这样。
Answers:
将徽章TextView
设置为a ,可以通过调用将数字值设置为所需的任何值setText()
。将的背景设置TextView
为XML<shape>
可绘制的图像,您可以使用该背景创建带边框的实心或渐变圆。一个XML可绘制对象将随着它或多或少的文本调整大小而缩放以适合该视图。
res / drawable / badge_circle.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#F00" />
<stroke
android:width="2dip"
android:color="#FFF" />
<padding
android:left="5dip"
android:right="5dip"
android:top="5dip"
android:bottom="5dip" />
</shape>
不过,您必须查看椭圆形/圆形如何以3-4位大数字缩放。如果不希望出现这种效果,请尝试如下所示的圆角矩形方法。如果数字较小,则矩形在半径会聚在一起时仍将看起来像一个圆形。
res / drawable / badge_circle.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="10dip"/>
<solid
android:color="#F00" />
<stroke
android:width="2dip"
android:color="#FFF" />
<padding
android:left="5dip"
android:right="5dip"
android:top="5dip"
android:bottom="5dip" />
</shape>
创建可伸缩背景后,您只需将其添加到的背景中TextView
,如下所示:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10"
android:textColor="#FFF"
android:textSize="16sp"
android:textStyle="bold"
android:background="@drawable/badge_circle"/>
最后,TextView
可以将这些徽标放置在布局中相应按钮/选项卡的顶部。我可能会通过将每个按钮及其徽章分组到一个RelativeLayout
容器中来做到这一点,如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/myButton"
android:layout_width="65dip"
android:layout_height="65dip"/>
<TextView
android:id="@+id/textOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/myButton"
android:layout_alignRight="@id/myButton"
android:text="10"
android:textColor="#FFF"
android:textSize="16sp"
android:textStyle="bold"
android:background="@drawable/badge_circle"/>
</RelativeLayout>
希望这些信息足以至少使您指明正确的方向!
oval
示例的版本,并添加<size>
元素以提供宽度/高度的比例
通过TextView
仅给样式赋予最简单的技巧。
<TextView
android:id="@+id/fabCounter"
style="@style/Widget.Design.FloatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="10dp"
android:padding="5dp"
android:text="10"
android:textColor="@android:color/black"
android:textSize="14sp" />
对于正在寻找Xamarin Android的人可以使用此代码
public class CountDrawable : Drawable
{
private float mTextSize;
private Paint mBadgePaint;
private Paint mTextPaint;
private Rect mTxtRect = new Rect();
private String mCount = "";
private bool mWillDraw = false;
public CountDrawable(Context context)
{
float mTextSize = context.Resources.GetDimension(Resource.Dimension.badge_count_textsize);
mBadgePaint = new Paint();
// mBadgePaint.SetCol(ContextCompat.GetColor(context.ApplicationContext, Resource.Color.background_color));
mBadgePaint.Color = new Color(Color.Red);
mBadgePaint.AntiAlias = true;
mBadgePaint.SetStyle(Paint.Style.Fill);
mTextPaint = new Paint();
mTextPaint.Color = new Color(Color.White);
mTextPaint.SetTypeface(Typeface.DefaultBold);
mTextPaint.TextSize = mTextSize;
mTextPaint.AntiAlias = true;
mTextPaint.TextAlign = Paint.Align.Center;
}
public override void Draw(Canvas canvas)
{
if(!mWillDraw)
{
return;
}
Rect bounds = GetBounds;
float width = bounds.Right - bounds.Left;
float height = bounds.Bottom - bounds.Top;
float radius = ((Math.Max(width, height) / 2)) / 2;
float centerX = (width - radius - 1) + 5;
float centerY = radius - 5;
if (mCount.Length <= 2)
{
// Draw badge circle.
canvas.DrawCircle(centerX, centerY, (int)(radius + 5.5), mBadgePaint);
}
else
{
canvas.DrawCircle(centerX, centerY, (int)(radius + 6.5), mBadgePaint);
}
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);
}
public Rect GetBounds { get; set; }
public void setCount(String count)
{
mCount = count;
// Only draw a badge if there are notifications.
// mWillDraw = !count.equalsIgnoreCase("0");
mWillDraw = !string.Equals(count, "0", StringComparison.OrdinalIgnoreCase);
// invalidateSelf();
}
public override void SetAlpha(int alpha)
{
}
public override void SetColorFilter(ColorFilter colorFilter)
{
}
public override int Opacity
{
get;
}
}
在MainActivity中
public override bool OnCreateOptionsMenu(IMenu menu)
{
// return base.OnCreateOptionsMenu(menu);
MenuInflater.Inflate(Resource.Menu.actionmenu, menu);
// var dd = menu.FindItem(Resource.Id.icon_group);
IMenuItem item = menu.FindItem(Resource.Id.ic_group);
LayerDrawable icon = item.Icon as LayerDrawable;
// LayerDrawable icon = (LayerDrawable)item.Icon;
CountDrawable badge;
Drawable reuse = icon.FindDrawableByLayerId(Resource.Id.ic_group_count);
if (reuse != null && reuse is CountDrawable)
{
badge = (CountDrawable)reuse;
}
else
{
badge = new CountDrawable(this);
}
badge.setCount("8");
badge.GetBounds=icon.Bounds;
icon.Mutate();
icon.SetDrawableByLayerId(Resource.Id.ic_group_count, badge);
return true;
}
只是添加。如果有人想使用环形而不是椭圆形来实现实心圆形气泡,这是将气泡计数添加到操作栏按钮的代码示例。但这可以添加到任何按钮。
(将其命名bage_circle.xml
):
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:useLevel="false"
android:thickness="9dp"
android:innerRadius="0dp"
>
<solid
android:color="#F00"
/>
<stroke
android:width="1dip"
android:color="#FFF" />
<padding
android:top="2dp"
android:bottom="2dp"/>
</shape>
您可能需要根据需要调整厚度。
结果将是这样的:
这是按钮的布局(命名为badge_layout.xml
):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.joanzapata.iconify.widget.IconButton
android:layout_width="44dp"
android:layout_height="44dp"
android:textSize="24sp"
android:textColor="@color/white"
android:background="@drawable/action_bar_icon_bg"
android:id="@+id/badge_icon_button"/>
<TextView
android:id="@+id/badge_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/badge_icon_button"
android:layout_alignRight="@id/badge_icon_button"
android:layout_alignEnd="@id/badge_icon_button"
android:text="10"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:paddingLeft="8dp"
android:gravity="center"
android:textColor="#FFF"
android:textSize="11sp"
android:background="@drawable/badge_circle"/>
</RelativeLayout>
在菜单中创建项目:
<item
android:id="@+id/menu_messages"
android:showAsAction="always"
android:actionLayout="@layout/badge_layout"/>
为了onCreateOptionsMenu
获得对菜单项的引用:
itemMessages = menu.findItem(R.id.menu_messages);
badgeLayout = (RelativeLayout) itemMessages.getActionView();
itemMessagesBadgeTextView = (TextView) badgeLayout.findViewById(R.id.badge_textView);
itemMessagesBadgeTextView.setVisibility(View.GONE); // initially hidden
iconButtonMessages = (IconButton) badgeLayout.findViewById(R.id.badge_icon_button);
iconButtonMessages.setText("{fa-envelope}");
iconButtonMessages.setTextColor(getResources().getColor(R.color.action_bar_icon_color_disabled));
iconButtonMessages.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (HJSession.getSession().getSessionId() != null) {
Intent intent = new Intent(getThis(), HJActivityMessagesContexts.class);
startActivityForResult(intent, HJRequestCodes.kHJRequestCodeActivityMessages.ordinal());
} else {
showLoginActivity();
}
}
});
收到消息通知后,设置计数:
itemMessagesBadgeTextView.setText("" + count);
itemMessagesBadgeTextView.setVisibility(View.VISIBLE);
iconButtonMessages.setTextColor(getResources().getColor(R.color.white));
此代码使用Iconify-fontawesome。
compile 'com.joanzapata.iconify:android-iconify-fontawesome:2.1.+'