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

不显示文字

//....

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 titleTextSize;

private int titleTextColor;

private String title;

private LayoutParams leftpParams,rightpParams,titlepParams;


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);


titleTextSize = ta.getDimension(R.styleable.TopBar_titleTextSize, 0);

titleTextColor = ta.getColor(R.styleable.TopBar_titleTextColor, 0);

title = ta.getString(R.styleable.TopBar_title);


ta.recycle();// 避免浪费资源,缓存引起的错误


// 1.获取自定义属性


leftButton = new Button(context);

rightButton = new Button(context);

tvTitle = new TextView(context);


// 2.创建控件


leftButton.setTextColor(leftTextColor);

leftButton.setBackground(leftBackground);

leftButton.setText(leftText);


rightButton.setTextColor(rightTextColor);

rightButton.setBackground(rightBackground);

rightButton.setText(rightText);


tvTitle.setTextColor(titleTextColor);

tvTitle.setTextSize(titleTextSize);

tvTitle.setText(title);

tvTitle.setGravity(Gravity.CENTER);


setBackgroundColor(0xfff59563);

leftpParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

leftpParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);

addView(leftButton, leftpParams);

rightpParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

rightpParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);

addView(rightButton, rightpParams);

titlepParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);

titlepParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);

addView(tvTitle, titlepParams);


}

}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res/com.example.topbar.TopBar"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >


    <com.example.topbar.TopBar

        android:layout_width="wrap_content"

        android:layout_height="40dp"

        app:leftText="left"

        app:rightText="right"

        app:title="自定义标题" >

    </com.example.topbar.TopBar>

</RelativeLayout>

atts代码:

<?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="leftText" format="string" />

        <attr name="leftBackGround" format="reference|color" />

        <attr name="leftTextColor" format="color" />

        <attr name="rightText" format="string" />

        <attr name="rightBackGround" format="reference|color" />

        <attr name="rightTextColor" format="color" />

    </declare-styleable>


</resources>


正在回答

5 回答

哥们,问题决解了吗,我倒腾了好久,还是不行。

0 回复 有任何疑惑可以回复我~
xmlns:app="http://schemas.android.com/apk/res/com.example.topbar.TopBar"

改成下面试试 :

xmlns:app="http://schemas.android.com/apk/res-auto"


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

感觉你没有设置布局方向  系统默认的是设置的是横向布局

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

qq_阳光下的躺椅 提问者

和布局方向没关系吧,就算是和布局方向有关,也应该显示,只是显示有问题而已。
2016-03-30 回复 有任何疑惑可以回复我~

上面只是补充的代码,刚开始没发现可以修改问题。抱歉。

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

atts代码:

<?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="leftText" format="string" />

        <attr name="leftBackGround" format="reference|color" />

        <attr name="leftTextColor" format="color" />

        <attr name="rightText" format="string" />

        <attr name="rightBackGround" format="reference|color" />

        <attr name="rightTextColor" format="color" />

    </declare-styleable>


</resources>


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

举报

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

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

进入课程

不显示文字

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