亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

自定義組件運行時出錯

//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模板設計
  • 參與學習       76032    人
  • 解答問題       233    個

快來學習如何在Android中自定義View,本次課程一定會讓你獲益匪淺

進入課程

自定義組件運行時出錯

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號