不能識別自定義控件中的屬性,自定義控件的三個子控件也不能正常顯示?
package com.jia.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.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* Created by Administrator on 2016/8/29.
*/
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 centerTitleTextSize;
? ?private int centerTitleTextColor;
? ?private String centerTitle;
? ?private LayoutParams leftParams,rightParams,centerTitleParams;
? ?//重寫構造方法
? ?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);
? ? ? ?centerTitleTextSize = ta.getDimension(R.styleable.TopBar_centerTitleTextSize, 0);
? ? ? ?centerTitleTextColor = ta.getColor(R.styleable.TopBar_centerTitleTextColor, 0);
? ? ? ?centerTitle = ta.getString(R.styleable.TopBar_centerTitle);
? ? ? ?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.setText(centerTitle);
? ? ? ?tvTitle.setTextColor(centerTitleTextColor);
? ? ? ?tvTitle.setTextSize(centerTitleTextSize);
? ? ? ?tvTitle.setGravity(Gravity.CENTER);
? ? ? ?setBackgroundColor(0xFFF59563);
? ? ? ?//把一個控件添加到ViewGroup
? ? ? ?leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
? ? ? ?leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
? ? ? ?addView(leftButton,leftParams);
? ? ? ?rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
? ? ? ?rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
? ? ? ?addView(rightButton,rightParams);
? ? ? ?centerTitleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
? ? ? ?centerTitleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
? ? ? ?addView(tvTitle,centerTitleParams);
? ?}
}
<?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:paddingBottom="@dimen/activity_vertical_margin" ????android:paddingLeft="@dimen/activity_horizontal_margin" ????android:paddingRight="@dimen/activity_horizontal_margin" ????android:paddingTop="@dimen/activity_vertical_margin" ????tools:context="com.jia.mytopbar.MainActivity"> ????<com.jia.mytopbar.TopBar ????????android:id="@+id/topbar" ????????android:layout_width="match_parent" ????????android:layout_height="40dp"> ????????custom:leftBackground="#ff08dd55" ????????custom:leftText="Back" ????????custom:leftTextColor="#ffffff" ????????custom:rightBackground="#ff08dd55" ????????custom:rightText="Back" ????????custom:rightTextColor="#ffffff" ????????custom:centerTitle="自定義標題" ????????custom:centerTitleTextColor="#123456" ????????custom:titleTextSize="16sp" ????</com.jia.mytopbar.TopBar> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
? ?<declare-styleable name="TopBar">
? ? ? ?<attr name="centerTitle" format="string" />
? ? ? ?<attr name="centerTitleTextSize" format="dimension" />
? ? ? ?<attr name="centerTitleTextColor" format="color" />
? ? ? ?<attr name="leftTextColor" format="color" />
? ? ? ?<attr name="leftBackground" format="reference|color" />
? ? ? ?<attr name="leftText" format="string" />
? ? ? ?<attr name="rightTextColor" format="color" />
? ? ? ?<attr name="rightBackground" format="reference|color" />
? ? ? ?<attr name="rightText" format="string" />
? ?</declare-styleable>
</resources>
2016-09-27
leftBackground和rightBackground是drawable類型,你這塊直接給了int類型。你可以在drawable文件夾中放置圖片或者定義drawable資源文件來實現
2016-09-18
請問,你的問題解決了么?我也是這個問題
2016-08-30
xml文件中對于安卓系統屬性的引用你沒寫 xmlns:android=" ?,這后面有一串引用地址的,不寫的話系統誤以為是xmlns:custom="http://schemas.android.com/apk/res-auto"這個地址