怎么在activity中实现android:drawableLeft

本文详细介绍了如何使用Android的TextView组件,并通过设置布局参数和复合绘制来实现个性化显示效果,包括设置TextView的布局方式、gravity属性以及使用ColorDrawable进行自定义边框绘制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TextView tv = new TextView(this);

tv.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));// 设置TextView的布局 1:weight


tv.setGravity(0x00000011);//设置gravity =center


Drawable drawable = new ColorDrawable(Color.GREEN);

drawable.setBounds(0, 0, 42, 32);
tv.setCompoundDrawables(drawable, null, null, null);//android:drawableLeft

<LinearLayout 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:orientation="vertical" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg" tools:context=".MainActivity"> <ImageView android:layout_width="match_parent" android:layout_height="85dp" android:src="@drawable/login" android:layout_marginTop="80dp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="MyMUSIC" android:layout_marginTop="20dp" android:textColor="#E62C21" android:gravity="center|center_vertical|center_horizontal" android:layout_marginBottom="65dp" android:textSize="25sp" android:textStyle="bold" /> <EditText android:id="@+id/login_account" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#B3FFFFFF" android:layout_marginLeft="50dp" android:layout_marginRight="50dp" android:layout_marginTop="10dp" android:hint="请输入账号" android:textSize="25sp" android:textStyle="bold" android:textColorHint="#FFFFFF" android:background="@drawable/edit_login_bg" android:padding="10dp" android:drawablePadding="10dp" android:drawableLeft="@drawable/icon_login" /> <EditText android:id="@+id/login_pwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#B3FFFFFF" android:layout_marginTop="20dp" android:layout_marginLeft="50dp" android:layout_marginRight="50dp" android:hint="请输入密码" android:textSize="25sp" android:textStyle="bold" android:textColorHint="#FFFFFF" android:background="@drawable/edit_login_bg" android:padding="10dp" android:drawablePadding="10dp" android:drawableLeft="@drawable/icon_pass" android:inputType="textPassword" /> <LinearLayout android:gravity="center_horizontal|center_vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="40dp" android:layout_marginLeft="50dp" android:layout_marginRight="50dp"> <Button android:id="@+id/login_denglu_btn" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="登录" android:layout_marginRight="20dp" android:background="@drawable/button_login_bg" android:textColor="#FDFDFD" android:foreground="?android:attr/selectableItemBackground" /> <Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="注册" android:layout_marginLeft="20dp" android:background="@drawable/button_login_bg" android:textColor="#FDFDFD" android:foreground="?android:attr/selectableItemBackground" />
03-16
<!-- 明细行布局 --> <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="4dp" android:background="@{position % 2 == 0 ? @color/white : @color/gray_100}"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="8dp"> <!-- 物料编码 --> <TextView android:id="@+id/tvMaterialCode" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1.5" android:text="MAT-001"/> <!-- 物料名称 --> <TextView android:id="@+id/tvMaterialName" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:text="物料A"/> <!-- 规格 --> <TextView android:id="@+id/tvSpecification" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1.5" android:text="标准"/> <!-- 数量 --> <TextView android:id="@+id/tvQuantity" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="100"/> <!-- 单价 --> <TextView android:id="@+id/tvUnitPrice" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="10.00"/> <!-- 金额 --> <TextView android:id="@+id/tvAmount" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="1000.00"/> <!-- 编辑按钮 --> <ImageButton android:id="@+id/btnEdit" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.5" android:src="@drawable/ic_edit"/> </LinearLayout> </androidx.cardview.widget.CardView> 帮我生成一下上面代码的预览结果
最新发布
07-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值