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

topbar颜色不改变

578c6bec0001167f05000185.jpg

setbackground出错 提示让改成setbackgrounddrawable

578c6bec0001e1e402320380.jpg

topbar.java



package com.example.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.View;

import android.view.ViewGroup;

import android.widget.Button;

import android.widget.RelativeLayout;

import android.widget.TextView;

import android.widget.Toast;


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

private int titleTextColor;

private String title;


private LayoutParams leftParams, rightParams, titleParams;

private topbarClickListener listener;


public interface topbarClickListener {

public void leftClick();


public void rightClick();


}


public void setOnTopbarClickListener(topbarClickListener listener) {

this.listener = listener;

}


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


tltleTextSize = ta.getDimension(R.styleable.Topbar_titleTextSize, 0);

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

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


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.setTextColor(titleTextColor);

tvTitle.setTextSize(tltleTextSize);

tvTitle.setText(title);

tvTitle.setGravity(Gravity.CENTER);


setBackgroundColor(0xff59563);


leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,

LayoutParams.WRAP_CONTENT);

rightParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);


addView(leftButton, leftParams);


rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,

LayoutParams.WRAP_CONTENT);

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


leftButton.setOnClickListener(new OnClickListener() {


@Override

public void onClick(View arg0) {

listener.leftClick();

}

});


rightButton.setOnClickListener(new OnClickListener() {


@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

listener.rightClick();

}

});

}

public void setLeftIsvisibable(boolean flag)

{

if(flag){

leftButton.setVisibility(View.VISIBLE);

}else{

leftButton.setVisibility(View.GONE);

}

}

}

  • mainactivity.java

package com.example.mytopbar;


import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.Toast;


public class MainActivity extends Activity {


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Topbar topbar=(Topbar)findViewById(R.id.topbar);

topbar.setOnTopbarClickListener(new Topbar.topbarClickListener() {

@Override

public void rightClick() {

// TODO Auto-generated method stub

Toast.makeText(MainActivity.this, "IMOOC_LEFT", Toast.LENGTH_SHORT).show();

}

@Override

public void leftClick() {

// TODO Auto-generated method stub

Toast.makeText(MainActivity.this, "IMOOC_RIGHT", Toast.LENGTH_SHORT).show();

}

});

}

//topbar.setLeftIsVisable(false);


}



  • activity_main.xml

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

    xmlns:custom="http://schemas.android.com/apk/res/com.example.mytopbar"

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

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:padding="5dp"

    tools:context="com.example.mytopbar.MainActivity" >


    <com.example.mytopbar.Topbar

        android:id="@+id/topbar"

        android:layout_width="match_parent"

        android:layout_height="40dp"

        custom:leftBackground="@drawable/blue_button"

        custom:leftText="Back"

        custom:leftTextColor="#FFFFFF"

        custom:rightBackground="@drawable/blue_button"

        custom:rightText="More"

        custom:rightTextColor="#FFFFFF"

        custom:title="自定义标题"

        custom:titleTextColor="#123412"

        custom:titleTextSize="10sp" >

    </com.example.mytopbar.Topbar>


</RelativeLayout>

  • atts.xml


<?xml version="1.0" encoding="utf-8"?>

<resources>


    <declare-styleable name="Topbar">

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

        <attr name="titleTextSize" format="dimension"></attr>

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

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

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

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

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

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

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

    </declare-styleable>


</resources>


正在回答

1 回答

Button 不是自己自定义的组件,所以要按系统的方法来设置属性.一般IDE提示怎么做,就按提示的来

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

举报

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

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

进入课程

topbar颜色不改变

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

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

帮助反馈 APP下载

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

公众号

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