Android自定义RelativeLayout

释放双眼,带上耳机,听听看~!

在工作中,有些界面比较类似或者需要重复使用。为了减低代码量,我们可以自定义成你想要的样子,比如下面这样:

我们可以继承RelativeLayout来拓展,这样既可以减轻代码量也可以更好的复用;

RelativelayoutTitleBar.java
package youli.com.example.administrator.relativelayout_zdy;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* Created by Administrator on 2019/11/18.
* <p>
* liuxiaodong
* <p>
* 自定义 Relativelayout
*/
public class RelativelayoutTitleBar extends RelativeLayout {
protected RelativeLayout leftLayout;
protected ImageView leftImage;
protected RelativeLayout rightLayout;
protected ImageView rightImage;
protected TextView titleView;
protected RelativeLayout titleLayout;
public RelativelayoutTitleBar(Context context) {
super(context);
}
public RelativelayoutTitleBar(Context context, AttributeSet attrs) {
super(context, attrs);
initview(context, attrs);
}
public RelativelayoutTitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initview(context, attrs);
}
private void initview(Context context, AttributeSet attrs) {
LayoutInflater.from(context).inflate(R.layout.ease_widget_title_bar, this);
leftLayout = (RelativeLayout) findViewById(R.id.left_layout);
leftImage = (ImageView) findViewById(R.id.left_image);
rightLayout = (RelativeLayout) findViewById(R.id.right_layout);
rightImage = (ImageView) findViewById(R.id.right_image);
titleView = (TextView) findViewById(R.id.title);
titleLayout = (RelativeLayout) findViewById(R.id.root);
parseStyle(context, attrs);
}
private void parseStyle(Context context, AttributeSet attrs) {
//这里相当于在xml里直接赋值-颜色、文字等
if (attrs != null) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RelativelayoutTitleBar);
//参数titleBarTitle给titleView设置文字
String title = ta.getString(R.styleable.RelativelayoutTitleBar_titleBarTitle);
titleView.setText(title);
//参数titleBarTitleDimens给titleView设置文字大小
float textSize = ta.getDimension(R.styleable.RelativelayoutTitleBar_titleBarTitleDimens, getResources().getDimension(R.dimen.size_16));
titleView.setTextSize(textSize);
//titleBarTitleTextColor给titleView设置字体颜色
int textColor = ta.getColor(R.styleable.RelativelayoutTitleBar_titleBarTitleTextColor, getResources().getColor(R.color.colorPrimary));
titleView.setTextColor(textColor);
//titleBarTitleTextColor给titleView设置背景颜色
Drawable titlebackground = ta.getDrawable(R.styleable.RelativelayoutTitleBar_titleBarBackgroundcolor);
if (null != titlebackground) {
titleView.setBackgroundDrawable(titlebackground);
}
//titleBarLeftImage给leftImage设置图片
Drawable leftDrawable = ta.getDrawable(R.styleable.RelativelayoutTitleBar_titleBarLeftImage);
if (null != leftDrawable) {
leftImage.setImageDrawable(leftDrawable);
}
//titleBarRightImage给rightImage设置图片
Drawable rightDrawable = ta.getDrawable(R.styleable.RelativelayoutTitleBar_titleBarRightImage);
if (null != rightDrawable) {
rightImage.setImageDrawable(rightDrawable);
}
//titleBarBackground给titleLayout设置背景颜色
Drawable background = ta.getDrawable(R.styleable.RelativelayoutTitleBar_titleBarBackground);
if (null != background) {
titleLayout.setBackgroundDrawable(background);
}
ta.recycle();  //回收TypedArray
}
}
/**
* 这里相当于在javawen文件中动态给值参数-颜色、文字等
*/
//给titleView设置字体大小
public void setTextSize(int size) {
titleView.setTextSize(size);
}
//给titleView设置字体颜色
public void setTextColor(int color) {
titleView.setTextColor(color);
}
//给titleView设置背景颜色
public void setbackgroundColor(int background) {
titleView.setBackgroundColor(background);
}
//给leftImage设置图片
public void setLeftImageResource(int resId) {
leftImage.setImageResource(resId);
}
//给rightImage设置图片
public void setRightImageResource(int resId) {
rightImage.setImageResource(resId);
}
//给leftLayout设置监听器
public void setLeftLayoutClickListener(OnClickListener listener) {
leftLayout.setOnClickListener(listener);
}
//给rightLayout设置监听器
public void setRightLayoutClickListener(OnClickListener listener) {
rightLayout.setOnClickListener(listener);
}
//给leftLayout设置显示或者隐藏
public void setLeftLayoutVisibility(int visibility) {
leftLayout.setVisibility(visibility);
}
//给rightLayout设置显示或者隐藏
public void setRightLayoutVisibility(int visibility) {
rightLayout.setVisibility(visibility);
}
//给titleView设置文字
public void setTitle(String title) {
titleView.setText(title);
}
//给titleLayout设置背景颜色
public void setBackgroundColor(int color) {
titleLayout.setBackgroundColor(color);
}
//获取leftLayout
public RelativeLayout getLeftLayout() {
return leftLayout;
}
//获取rightLayout
public RelativeLayout getRightLayout() {
return rightLayout;
}
}
ease_widget_title_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="46dp"
android:background="#00acff"
android:gravity="center_vertical" >
<RelativeLayout
android:id="@+id/left_layout"
android:layout_width="50dip"
android:layout_height="match_parent"
android:background="@drawable/ease_common_tab_bg"
android:clickable="true" >
<ImageView
android:id="@+id/left_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="centerInside" />
</RelativeLayout>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#ffffff"
android:textSize="20sp" />
<RelativeLayout
android:id="@+id/right_layout"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="@drawable/ease_common_tab_bg" >
<ImageView
android:id="@+id/right_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="centerInside" />
</RelativeLayout>
</RelativeLayout>

样式-自定义参数

    <declare-styleable name="RelativelayoutTitleBar">
<attr name="titleBarTitle" format="string"/>
<attr name="titleBarLeftImage" format="reference"/>
<attr name="titleBarRightImage" format="reference"/>
<attr name="titleBarBackground" format="reference|color"/>
<attr name="titleBarBackgroundcolor" format="reference|color"/>
<attr name="titleBarTitleTextColor" format="color"/>
<attr name="titleBarTitleDimens" format="dimension"/>
</declare-styleable>

OK到这已经自定义完成,怎么使用呢?

MainActivity.java
package youli.com.example.administrator.relativelayout_zdy;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
RelativelayoutTitleBar title_bar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
title_bar = findViewById(R.id.title_bar);
//动态给值
title_bar.setTextSize(60);
title_bar.setTextColor(Color.RED);
//监听器
title_bar.setRightLayoutClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"返回",Toast.LENGTH_SHORT).show();
}
});
title_bar.getRightLayout().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"删除",Toast.LENGTH_SHORT).show();
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:hyphenate="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="youli.com.example.administrator.relativelayout_zdy.MainActivity">
<youli.com.example.administrator.relativelayout_zdy.RelativelayoutTitleBar
android:id="@+id/title_bar"
android:layout_width="match_parent"
android:layout_height="50dp"
hyphenate:titleBarLeftImage="@drawable/ease_mm_title_back"
hyphenate:titleBarRightImage="@drawable/ease_mm_title_remove"
hyphenate:titleBarTitle="猪年大吉"
hyphenate:titleBarBackground="#006677"
hyphenate:titleBarBackgroundcolor="#018288"
hyphenate:titleBarTitleTextColor="#3f86ff"
/>
</RelativeLayout>

搞定,本来需要四个控件现在只用一个即可实现

如果不需要返回或者删除按钮可以影藏

    public void setLeftLayoutVisibility(int visibility) {
leftLayout.setVisibility(visibility);
}

人已赞赏
Android文章

网络问题引起的Corodva Could not resolve com.android.tools.build:gradle:3.0.0

2020-2-28 22:25:08

Android文章

Android布局中动态添加ImageView

2020-2-29 5:25:05

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索