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

不能识别自定义控件中的属性,自定义控件的三个子控件也不能正常显示?

package com.jia.mytopbar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
* Created by Administrator on 2016/8/29.
*/
public class TopBar extends RelativeLayout {

   private Button leftButton,rightButton;
   private TextView tvTitle;
   
   private int leftTextColor;
   private Drawable leftBackground;
   private String leftText;
   private int rightTextColor;
   private Drawable rightBackground;
   private String rightText;
   private float centerTitleTextSize;
   private int centerTitleTextColor;
   private String centerTitle;

   private LayoutParams leftParams,rightParams,centerTitleParams;

   //重写构造方法
   public TopBar(Context context, AttributeSet attrs) {
       super(context, attrs);
       //取值
       TypedArray ta = context.obtainStyledAttributes(attrs,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);

       centerTitleTextSize = ta.getDimension(R.styleable.TopBar_centerTitleTextSize, 0);
       centerTitleTextColor = ta.getColor(R.styleable.TopBar_centerTitleTextColor, 0);
       centerTitle = ta.getString(R.styleable.TopBar_centerTitle);

       ta.recycle();
       //组合模式,定义已有组件,拼合
       leftButton = new Button(context);
       rightButton = new Button(context);
       tvTitle = new TextView(context);
       
       leftButton.setTextColor(leftTextColor);
       leftButton.setBackground(leftBackground);
       leftButton.setText(leftText);

       rightButton.setTextColor(rightTextColor);
       rightButton.setBackground(rightBackground);
       rightButton.setText(rightText);

       tvTitle.setText(centerTitle);
       tvTitle.setTextColor(centerTitleTextColor);
       tvTitle.setTextSize(centerTitleTextSize);
       tvTitle.setGravity(Gravity.CENTER);

       setBackgroundColor(0xFFF59563);

       //把一个控件添加到ViewGroup
       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);

       centerTitleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
       centerTitleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
       addView(tvTitle,centerTitleParams);

   }
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.jia.mytopbar.MainActivity">

    <com.jia.mytopbar.TopBar

        android:id="@+id/topbar"
        android:layout_width="match_parent"
        android:layout_height="40dp">
        custom:leftBackground="#ff08dd55"
        custom:leftText="Back"
        custom:leftTextColor="#ffffff"
        custom:rightBackground="#ff08dd55"
        custom:rightText="Back"
        custom:rightTextColor="#ffffff"
        custom:centerTitle="自定义标题"
        custom:centerTitleTextColor="#123456"
        custom:titleTextSize="16sp"
    </com.jia.mytopbar.TopBar>
</RelativeLayout>

http://img1.sycdn.imooc.com//57c6da710001b9d111920676.jpg


<?xml version="1.0" encoding="utf-8"?>
<resources>
   <declare-styleable name="TopBar">
       <attr name="centerTitle" format="string" />
       <attr name="centerTitleTextSize" format="dimension" />
       <attr name="centerTitleTextColor" 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>


正在回答

3 回答

leftBackground和rightBackground是drawable类型,你这块直接给了int类型。你可以在drawable文件夹中放置图片或者定义drawable资源文件来实现

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

请问,你的问题解决了么?我也是这个问题

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

流沙009 提问者

还没解决,你如果解决共享一下
2016-09-22 回复 有任何疑惑可以回复我~

xml文件中对于安卓系统属性的引用你没写 xmlns:android="  ,这后面有一串引用地址的,不写的话系统误以为是xmlns:custom="http://schemas.android.com/apk/res-auto"这个地址

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

举报

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

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

进入课程

不能识别自定义控件中的属性,自定义控件的三个子控件也不能正常显示?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信