Android开发:用自定义ViewGroup统一设置多View的textSize和textColor
在Android开发中,如果你想统一设置多个View的android:textSize和android:textColor,你可以使用自定义的ViewGroup。自定义的ViewGroup是一个继承自ViewGroup的类,它可以帮助你管理多个子View。 以下是一个简单的例子: ```java public class CustomTextViewGroup extends ViewGroup { private Paint mPaint; private int mDefaultTextSize; private int mDefaultTextColor; public CustomTextViewGroup(Context context) { super(context); mPaint = new Paint(); mDefaultTextSize = 16; mDefaultTextColor = Color.BLACK; } public CustomTextViewGroup(Context context, AttributeSet attrs) { super(context, attrs); mPaint = new Paint(); mDefaultTextSize = 16; mDefaultTextColor = Color.BLACK; TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomTextViewGroup); mDefaultTextSize = a.getDimensionPixelSize(R.styleable.CustomTextViewGroup_textSize, mDefaultTextSize); mDefaultTextColor = a.getColor(R.styleable.CustomTextViewGroup_textColor, mDefaultTextColor); a.recycle(); } public CustomTextViewGroup(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mPaint = new Paint(); mDefaultTextSize = 16; mDefaultTextColor = Color.BLACK; TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomTextViewGroup); mDefaultTextSize = a.getDimensionPixelSize(R.styleable.CustomTextViewGroup_textSize, mDefaultTextSize); mDefaultTextColor = a.getColor(R.styleable.CustomTextViewGroup_textColor, mDefaultTextColor); a.recycle(); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); for (int i = 0; i < getChildCount(); i++) { View child = getChildAt(i); child.setTextSize(TypedValue.COMPLEX_UNIT_PX, mDefaultTextSize); child.setTextColor(mDefaultTextColor); } } } ``` 在这个例子中,我们创建了一个名为CustomTextViewGroup的自定义ViewGroup。它有一个Paint对象用于设置文本的颜色和大小,以及默认的文本大小和颜色。 在onLayout方法中,我们遍历所有的子View,将它们的文本大小和颜色设置为我们预设的值。 然后,你可以在你的布局文件中使用这个自定义的ViewGroup,就像使用普通的TextView一样: ```xml <com.example.CustomTextViewGroup android:layout_width="wrap_content" android:layout_height="wrap_content" app:textSize="20sp" app:textColor="@color/colorPrimary" /> ``` 在这个例子中,我们将文本大小设置为20sp,文本颜色设置为主题色(colorPrimary)。 ######[AI写代码神器 | 600点数解答 | 2023-11-07 22:47:08]
- RealtimeMonitoringFragment 代码片段:Android 实时监控功能核心代码剖析(字节豆包 | 30点数解答 | 2024-10-12 10:31:18)294
- 揭秘:Android实时监控片段(RealTimeMonitoringFragment)代码实现(GPT | 531点数解答 | 2024-10-12 10:32:25)269
- 代码改造:巧用vw和vh让代码适配不同屏幕大小(讯飞星火 | 623点数解答 | 2024-08-23 11:41:50)288
- 代码分析:单据信息模板代码优化与逻辑注意点(字节豆包 | 154点数解答 | 2025-01-15 10:42:58)342
- Vue 模板代码展示与优化:单据信息呈现及注意要点(字节豆包 | 155点数解答 | 2025-01-15 10:42:59)254
- AIDE编写Xposed软件:参考代码教你拦截Smail类方法名(阿里通义 | 649点数解答 | 2024-10-12 00:15:48)305
- Vue.js 模板:打造含签名框的报废审批列表(阿里通义 | 553点数解答 | 2024-09-25 16:57:13)369
- Android开发:用自定义ViewGroup统一设置多View的textSize和textColor(阿里通义 | 600点数解答 | 2023-11-07 22:47:08)185
- Android开发自定义 Handler :七大策略有效规避内存泄漏!(百度文心 | 526点数解答 | 2023-11-09 17:34:21)285
- 智能电话窗口按钮1弹起:获取坐标与手机IMEI码待处理(GPT | 208点数解答 | 2024-09-12 16:36:31)353
- Linux开发:CentOS 6.x 中修改 /etc/inittab 设置默认运行级别及新版系统应对建议 (百度文心 | 281点数解答 | 2023-11-09 18:42:13)314
- Python 字典基本操作全解析:创建、查找与遍历示例( | 313点数解答 | 2024-05-06 09:52:01)377