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

左右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模板设计
  • 参与学习       76061    人
  • 解答问题       223    个

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

进入课程

左右Button不出现,有人知道怎么解决吗?

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