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

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

不能識別自定義控件中的屬性,自定義控件的三個子控件也不能正常顯示?

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>

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


<?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>


正在回答

3 回答

leftBackground和rightBackground是drawable類型,你這塊直接給了int類型。你可以在drawable文件夾中放置圖片或者定義drawable資源文件來實現

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

請問,你的問題解決了么?我也是這個問題

0 回復 有任何疑惑可以回復我~
#1

流沙009 提問者

還沒解決,你如果解決共享一下
2016-09-22 回復 有任何疑惑可以回復我~

xml文件中對于安卓系統屬性的引用你沒寫 xmlns:android=" ?,這后面有一串引用地址的,不寫的話系統誤以為是xmlns:custom="http://schemas.android.com/apk/res-auto"這個地址

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

舉報

0/150
提交
取消
Android UI模板設計
  • 參與學習       76032    人
  • 解答問題       233    個

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

進入課程

不能識別自定義控件中的屬性,自定義控件的三個子控件也不能正常顯示?

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

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

幫助反饋 APP下載

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

公眾號

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