我正在动态创建按钮.我首先使用XML设置它们,我正在尝试使用下面的XML并使其编程.
<Button
android:id="@+id/buttonIdDoesntMatter"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="buttonName"
android:drawableLeft="@drawable/imageWillChange"
android:onClick="listener"
android:layout_width="fill_parent">
</Button>
Run Code Online (Sandbox Code Playgroud)
这就是我到目前为止所拥有的.除了可绘制之外,我可以做任何事情.
linear = (LinearLayout) findViewById(R.id.LinearView);
Button button = new Button(this);
button.setText("Button");
button.setOnClickListener(listener);
button.setLayoutParams(
new LayoutParams(
android.view.ViewGroup.LayoutParams.FILL_PARENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT
)
);
linear.addView(button);
Run Code Online (Sandbox Code Playgroud) 根据我的代码运行新的Lint工具.它提出了很多好的建议,但这个我无法理解.
此标签及其子代可以替换为一个和复合drawable
问题:检查当前节点是否可以使用复合drawables替换为TextView.
包含ImageView和TextView的LinearLayout可以作为复合drawable更有效地处理
这是我的布局
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerInParent="true">
<ImageView
android:id="@+id/upImage"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:scaleType="centerInside"
android:src="@drawable/up_count_big">
</ImageView>
<TextView
android:id="@+id/LikeCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginBottom="1dp"
android:textColor="@color/gray"
android:textSize="16sp"
android:layout_gravity="center_vertical">
</TextView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,有人能提供一个如何制作复合可绘制的具体例子吗?
android textview android-layout android-linearlayout compound-drawables
我是开发人员.我需要实现如下所示的设计.我已经有功能的应用程序,但想知道如何处理这个?特别是,我对如何在标签下显示"新"项数量感兴趣.我知道怎么做 - 用红点创建新图标,只在有新东西时显示它们.
但是我不知道如何让这些圆圈漂浮在标题之上并在里面显示数字.有人建议过什么吗?样品?路线?
关于分离活动的第二个问题 我应该控制组合这样的按钮,只是在活动上充气吗?否则我可能会创建选项卡式活动,但我不确定是否可以设置它以使其看起来像这样.

我的主要活动中有此功能,但不显示徽章:
private fun setFindShiftBadge(state: HomeState) {
val findShiftsBadge = BadgeDrawable.create(this)
home_framelayout.foreground = findShiftsBadge
findShiftsBadge.badgeGravity = BadgeDrawable.TOP_END
findShiftsBadge.backgroundColor = resources.getColor(R.color.colorWhite)
findShiftsBadge.badgeTextColor = resources.getColor(R.color.colorPrimary)
findShiftsBadge.number = state.availableShifts.size
}
Run Code Online (Sandbox Code Playgroud)
在同一个活动中,我有这个显示徽章的功能:
private fun setMyPostedShiftBadge(state: HomeState) {
val userShiftsBadge: BadgeDrawable =
bottom_navigation_view.getOrCreateBadge(R.id.bottom_navigation_my_posted_shifts_text)
userShiftsBadge.number = state.userShifts.size
userShiftsBadge.backgroundColor = resources.getColor(R.color.colorPrimary)
}
Run Code Online (Sandbox Code Playgroud)
现在我明白第二个函数可以工作,因为徽章设置在BottomNavigationView. 具有讽刺意味的是,BottomNavigationView扩展FrameLayout。在 Android 文档中:
使用 attachBadgeDrawable(BadgeDrawable, View, FrameLayout) 将 BadgeDrawable 作为 ViewOverlay 添加到所需的锚视图。使用 updateBadgeCoordinates(View, ViewGroup) 根据锚视图更新 BadgeDrawable BadgeDrawable 的坐标(中心和边界)。
他们说要使用这段代码,For API 18+ (APIs supported by ViewOverlay)我在第一个不起作用的函数中使用了它:
BadgeDrawable badgeDrawable …Run Code Online (Sandbox Code Playgroud) android badge android-view android-drawable android-framelayout