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

自定义组件运行时出错

//Topbar.java文件

package com.example.myview;


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;


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


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();// ������Դ


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.setTextSize(titleTextSize);

tvTitle.setTextColor(titleTextColor);

tvTitle.setText(title);

tvTitle.setGravity(Gravity.CENTER);


setBackgroundColor(0x12321);


leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,

LayoutParams.WRAP_CONTENT);// �������

leftParams.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 v) {

// TODO Auto-generated method stub

listener.leftClick();

}

});


rightButton.setOnClickListener(new OnClickListener() {


@Override

public void onClick(View v) {

// TODO Auto-generated method stub

listener.rightClick();

}

});

}


}

//布局文件

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

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

    xmlns:customer="http://schemas.android.com/apk/res/com.example.view"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" 

    >

    

    <TextView 

        android:layout_width="wrap_content"

        android:layout_height="40dp"

        android:layout_marginTop="50dp"

        android:text="@string/app_name"

        android:background="#123432"

        />

    

<com.example.myview.Topbar

   android:id="@+id/tb"

   android:layout_width="match_parent"

   android:layout_height="40dp"

   customer:leftBackground="#123212"

   customer:leftText="Back"

   customer:leftTextColor="#FFFFFF"

   customer:rightBackground="#abc321"

   customer:rightText="More"

   customer:rightTextColor="#FFFFFF"

   customer:titleTextColor="#123412"

   customer:titleTextSize="10sp"

   customer:title="波波" />


</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 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"/>

        <!-- reference 引用资源文件 -->

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

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

    </declare-styleable>

</resources>


代码没报错  但是运行出错。静态时运行没问题,但是加上监听事件就运行出错。

正在回答

1 回答

new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,

LayoutParams.MATCH_PARENT);

ViewGroup改成Relativelayout可以不

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

举报

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

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

进入课程

自定义组件运行时出错

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