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

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

左右Button不出現,有人知道怎么解決嗎?

package com.example.topbar;


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;


public class Topbar extends RelativeLayout{

private Button leftButton,rightButton;

private TextView tvTitle;

//左邊button的屬性

private int leftTextColor;

private Drawable leftBackground;

private String leftText;

//右邊button的屬性

private int rightTextColor;

private Drawable rightBackground;

private String rightText;

//Title屬性

private float titleTextSize;

private int titleTextColor;

private String title;

private LayoutParams leftParams,rightParams,titleParams;//布局屬性


public Topbar(Context context, AttributeSet attrs) {

super(context, attrs);

// 思路:在構造方法中獲取我們在xml文件自定義的那些屬性

? ?//并把這些屬性值賦給我們的控件

//TypedArray是一個數據結構

//context.obtainStyledAttributes(set, attrs)傳入xml文件中的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();//回收數據:1.避免浪費資源;2、避免由于換成你而發生錯誤

//實例化

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(0xFFF59563);//給viewGroup設置背景顏色

?

leftParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);//傳入長寬值

leftParams.addRule(RelativeLayout.ALIGN_LEFT,TRUE);//給leftParams添加一個規則:居左對齊

addView(leftButton, leftParams);//調用ViewGroup中的addView方法,使得leftButton以leftParams的形式加入到viewGroup中

?

rightParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

rightParams.addRule(RelativeLayout.ALIGN_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);

}


}



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

? ? xmlns:custom="http://schemas.android.com/apk/res-com.example.topbar.Topbar"

? ? 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.example.topbar.MainActivity" >


? ? <com.example.topbar.Topbar?

? ? ? ? android:id="@+id/topbar"

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="50dp"

? ? ? ? custom:leftText="Back"

? ? ? ? custom:leftBackground="@drawable/leftbutton"

? ? ? ? custom:leftTextColor="#FFFFFF"

? ? ? ? custom:rightText="More"

? ? ? ? custom:rightBackground="@drawable/leftbutton"

? ? ? ? custom:rightTextColor="#FFFFFF"

? ? ? ? custom:title="自定義標題"

? ? ? ? custom:titleTextSize="10sp"

? ? ? ? custom:titleTextColor="#123412"

? ? ? ??

? ? ? ??

? ? ? ? ></com.example.topbar.Topbar>


</RelativeLayout>

http://img1.sycdn.imooc.com//56f78fc300019b9a02970343.jpg

正在回答

3 回答

在addRule這里,參數你寫錯了。

leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);


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

慕仔2299439

贊!我也遇到了這個問題,就是這里的參數沒加PARENT
2016-07-11 回復 有任何疑惑可以回復我~

和你一樣

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

在模擬器上運行看看效果

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

qq_溫暖遙遠不過時光_0 提問者

模擬器上也不行
2016-03-27 回復 有任何疑惑可以回復我~

舉報

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

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

進入課程

左右Button不出現,有人知道怎么解決嗎?

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

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

幫助反饋 APP下載

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

公眾號

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