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

完全按照视频来的,但是运行出来左右button都不显示,有人知道怎么解决么?

package com.example.acer.topbar_demo;

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;


public class Topbar extends RelativeLayout {

    private Button leftButton;
    private Button rightButton;
    private TextView tvTitle;

    private int leftTextColor;
    private String leftText;
    private Drawable leftBackground;

    private  int rightTextColor;
    private  String rightText;
    private  Drawable rightBackground;

    private  float titleTextSize;
    private  int mTitleTextColor;
    private  String titleText;

    private  LayoutParams leftParams, rightParams, titleParams;
    public Topbar(Context context, AttributeSet attr){
        super(context,attr);

        TypedArray ta = context.obtainStyledAttributes(attr,R.styleable.Topbar);
        leftTextColor = ta.getColor(R.styleable.Topbar_leftTextColor, 0);
        leftText = ta.getString(R.styleable.Topbar_leftText);
        leftBackground = ta.getDrawable(R.styleable.Topbar_leftBackground);

        titleTextSize = ta.getDimension(R.styleable.Topbar_titleTextSize, 0);
        titleText = ta.getString(R.styleable.Topbar_title);
        mTitleTextColor = ta.getColor(R.styleable.Topbar_mTitleTextColor, 0);

        rightText = ta.getString(R.styleable.Topbar_rightText);
        rightTextColor = ta.getColor(R.styleable.Topbar_rightTextColor, 0);
        rightBackground = ta.getDrawable(R.styleable.Topbar_rightBackground);

        ta.recycle();

        leftButton = new Button(context);
        rightButton = new Button(context);
        tvTitle = new TextView(context);

        leftButton.setTextColor(leftTextColor);
        leftButton.setBackground(leftBackground);
        leftButton.setText(leftText);

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

        tvTitle.setTextColor(mTitleTextColor);
        tvTitle.setText(titleText);
        tvTitle.setTextSize(titleTextSize);
        tvTitle.setGravity(Gravity.CENTER);

        setBackgroundColor(0xFFF59563);

        leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);

        addView(leftButton, leftParams);

        rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);

        addView(rightButton, rightParams);

        titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);

        addView(tvTitle, titleParams);
    }
}


<?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:padding="5dp"
    tools:context="com.example.acer.topbar_demo.MainActivity">

    <!--<TextView-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:text="Hello World!" />-->
    <android.support.v7.widget.Toolbar
        android:id="@+id/topbar"
        android:layout_width="match_parent"
        android:layout_height="40dp"

        custom:leftBackground="@color/colorAccent"
        custom:leftText="Back"
        custom:leftTextColor="#ffffff"

        custom:rightBackground="@color/colorPrimaryDark"
        custom:rightText="More"
        custom:rightTextColor="#ffffff"

        custom:title="Myself"
        custom:mTitleTextColor="#ffffff"
        custom:titleTextSize="10dp">

    </android.support.v7.widget.Toolbar>
</RelativeLayout>


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="title" format="string"/>
    <attr name="titleTextSize" format="dimension"/>
    <attr name="mTitleTextColor" format="color"/>
    <declare-styleable name="Topbar">
        <attr name="title"/>
        <attr name="titleTextSize"/>
        <attr name="mTitleTextColor"/>

        <attr name="leftTextColor" format="color"/>
        <attr name="leftText" format="string"/>
        <attr name="leftBackground" format="reference|color"/>

        <attr name="rightText" format="string"/>
        <attr name="rightBackground" format="reference|color"/>
        <attr name="rightTextColor" format="color"/>
    </declare-styleable>
</resources>


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

正在回答

2 回答

这块<android.support.v7.widget.Toolbar
       
    </android.support.v7.widget.Toolbar>

你是不是引用错了呀?应该引用的是你自定义的

<com.example.mytopbar.Topbar
  
</com.example.mytopbar.Topbar>

而不是系统自带的。

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

接口没有看到啊

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

举报

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

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

进入课程

完全按照视频来的,但是运行出来左右button都不显示,有人知道怎么解决么?

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