首页 > 编程知识 正文

android studio 怎么设置preview,android怎样改变textview的颜色

时间:2023-05-03 16:54:49 阅读:259241 作者:4036

qq_慕妹9571422015-12-23 23:42package com.im.imdemo.view;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.ColorFilter;import android.graphics.drawable.ColorDrawable;import android.graphics.drawable.Drawable;import android.text.Layout;import android.util.AttributeSet;import android.view.Gravity;import android.view.ViewGroup;import android.widget.Button;import android.widget.swdgs;import android.widget.TextView;import com.im.imdemo.R;/**public class CustomTopBar extends swdgs {private Button btn_left,btn_right;private TextView tv_title;private String titleText;private float titleTextSize;private int titleTextColor;private String leftBtnText;private int leftBtnColor;private Drawable leftBtnbackground;private String rightBtnText;private int rightBtnColor;private Drawable rightBtnbackground;private LayoutParams leftParams,rightParams,titleParams;public CustomTopBar(Context context) {super(context);}public CustomTopBar(Context context, AttributeSet attrs) {super(context, attrs);InitView(context, attrs);}private  void InitView(Context context, AttributeSet attributeSet){ta.recycle(); //释放TypedArray 资gxdhb/pryddsp>setBackgroundColor(0xED280E);//实例化Viewbtn_left = new Button(context);btn_right = new Button(context);tv_title = new TextView(context);//设置View属性btn_left.setText(leftBtnText);btn_left.setTextColor(leftBtnColor);btn_left.setBackground(leftBtnbackground);btn_right.setText(rightBtnText);btn_right.setTextColor(rightBtnColor);btn_right.setBackground(rightBtnbackground);tv_title.setText(titleText);tv_title.setTextColor(titleTextColor);tv_title.setTextSize(titleTextSize);tv_title.setGravity(Gravity.CENTER);//设置View的布局leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);leftParams.addRule(swdgs.ALIGN_PARENT_LEFT, TRUE);addView(btn_left, leftParams);rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);rightParams.addRule(swdgs.ALIGN_PARENT_RIGHT,TRUE);addView(btn_right, rightParams);titleParams = new LayoutParams(swdgs.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);titleParams.addRule(swdgs.CENTER_IN_PARENT,TRUE);addView(tv_title,titleParams);}}<?xml  version="1.0" encoding="utf-8"?>

xmlns:android="http://schemas.android.com/apk/res/android"xmlns:custom = "http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.im.imdemo.ui.ContentActivity">

android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@color/colorAccent"custom:topBarText="Hello"custom:topBarTextSize="10sp"custom:topBarTextColor="#0A0909"custom:leftBackground="@color/colorAccent"custom:leftBtnText="返回"custom:leftBtnTextColor="@color/blue"custom:rightBtnText="添加"custom:rightBtnTextColor="@color/blue"custom:rightBackground="@color/colorAccent"android:layout_alignParentRight="true">在代码中使用setBackgroundColor(0xED280E); 控件得背景色并没用换色,并且在xml文件中使用android:background="@color/colorAccent"设置背景色也不起作用,如果把setBackgroundColor(0xED280E);这句话删除的话就可以了。实在搞不懂是怎么回事?

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。