为了账号安全,请及时绑定邮箱和手机立即绑定

显示不出来效果

public class TopBar extends RelativeLayout {

	private Button leftButton;//左边按钮
	private Button rightButton;//右边按钮
	private TextView tvTitle;   //中间标题
	
	//声明一些控件的属性
	private int leftTextColor;
	private Drawable leftBackground;
	private String leftText;
	
	private int rightTextColor;
	private Drawable rightBackground;
	private String rightText;
	
	private int titleTextColor;
	private String  title;
	private float titleTextSize;
	
	//为三个布局形式
	private LayoutParams leftParams,rightParams,titleParams;
	
	@SuppressLint("NewApi") public TopBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		Log.i("info", "调用了自定义的属性");
		//拿到自己定义的属性
		TypedArray ta=context.obtainStyledAttributes(R.styleable.TopBar);
		
		leftTextColor=ta.getColor(R.styleable.TopBar_leftTextColor, 0);
		leftBackground=ta.getDrawable(R.styleable.TopBar_leftBackground);
		leftText=ta.getString(R.styleable.TopBar_leftText);
		
		rightTextColor=ta.getColor(R.styleable.TopBar_rightTextColor, 0);
		rightBackground=ta.getDrawable(R.styleable.TopBar_rightBackground);
		rightText=ta.getString(R.styleable.TopBar_rightText);
		
		titleTextColor=ta.getColor(R.styleable.TopBar_titleTextColor, 0);
		title=ta.getString(R.styleable.TopBar_title);
		titleTextSize=ta.getDimension(R.styleable.TopBar_titleTextSize, 15);
				
		//回收掉  1、有可能浪费资源。2、避免由于一些直的缓存发生的错误
		ta.recycle();
		
		//创建控件
		leftButton=new Button(context);
		rightButton=new Button(context);
		tvTitle=new TextView(context);
		
		//为三个空间设置属性
		leftButton.setTextColor(leftTextColor);
		leftButton.setBackground(leftBackground);//注意最小的sdk为16
		leftButton.setText(leftText);
		
		rightButton.setTextColor(rightTextColor);
		rightButton.setBackground(rightBackground);//注意最小的sdk为16
		rightButton.setText(rightText);
		
		tvTitle.setText(title);
		tvTitle.setTextColor(titleTextColor);
		tvTitle.setTextSize(titleTextSize);
		tvTitle.setGravity(Gravity.CENTER);//自己添加的属性使其居中
		
		//设置整体布局的背景颜色
		setBackgroundColor(0x6699ff);
		
		//为左部局参数
		//第一个属性为宽,第二个为高
		leftParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
		leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
		//添加控件
		addView(leftButton,leftParams);
		
		//为右边局参数
		rightParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
		rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT ,TRUE);
		//添加控件
		addView(rightButton,rightParams);			
				
		//为标题参数
		titleParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
		titleParams.addRule(RelativeLayout.CENTER_IN_PARENT ,TRUE);
		//添加控件
		addView(tvTitle,titleParams);
	}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
     <declare-styleable name="TopBar">
         
         <attr name="title" format="string"/>
         <attr name="titleTextSize" format="dimension"/>
         <attr name="titleTextColor" format="color"/>
         <attr name="leftTextColor" format="color"/>
         <attr name="leftBackground" format="reference|color"/>
         <attr name="leftText" format="string"/>
         <attr name="rightTextColor" format="color"/>
         <attr name="rightBackground" format="reference|color"/>
         <attr name="rightText" format="string"/>
         
     </declare-styleable>
    
</resources>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res/com.fdd.topbar">

    <com.fdd.topbar.TopBar
        android:id="@+id/topbar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:leftBackground="#000000"
        app:leftText="左部按钮"
        app:leftTextColor="#ff0000"
        app:rightBackground="#00000f"
        app:rightText="右部按钮"
        app:rightTextColor="#ff00ff"
        app:title="标题"
        app:titleTextColor="#00ff00"
        app:titleTextSize="25sp" />

</RelativeLayout>


正在回答

3 回答

是改成auto  老师说elipse不是用auto的   可是事实是要用auto  

0 回复 有任何疑惑可以回复我~

应该改成xmlns:app="http://schemas.android.com/apk/res-auto,我复制没注意^_^

0 回复 有任何疑惑可以回复我~
#1

冯冬冬 提问者

还是不行
2016-12-28 回复 有任何疑惑可以回复我~

在xml布局引用中,自定义命名空间app写错了,可以改为xmlns:android="http://schemas.android.com/apk/res-auto,同学记得多注意些重要细节哦!

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Android UI模板设计
  • 参与学习       76030    人
  • 解答问题       233    个

快来学习如何在Android中自定义View,本次课程一定会让你获益匪浅

进入课程

显示不出来效果

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号