其他分享
首页 > 其他分享> > Android 自定义View:TopBar(1),androidstudio蓝牙应用开发

Android 自定义View:TopBar(1),androidstudio蓝牙应用开发

作者:互联网

下载地址:https://github.com/zhuanghongji/CustomViewZhj

博客中只是部分演示代码,后面新增演示的代码均只在github上更新。

效果图

===

代码展示:

=====

MainActivity.java :


import com.zhuanghongji.customviewzhj.view.TopBar;

pub
lic class MainActivity extends AppCompatActivity {

TopBar mTopBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTopBar = (TopBar) findViewById(R.id.topBar);

// mTopBar.setLeftButtonIsVisible(false);

    mTopBar.setOnTopBarClickListener(new TopBar.TopBarClickListener() {
        @Override
        public void leftBtnClick() {
            Toast.makeText(MainActivity.this, " 点击了 leftBtn ", Toast.LENGTH_SHORT).show();
        }
    });
}

}

activity_main.xml :


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

xmlns:tools="http://schemas.android.com/tools"
xmlns:zhj="http://schemas.android.com/apk/res-auto"
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=".MainActivity">
<com.zhuanghongji.customviewzhj.view.TopBar
    android:id="@+id/topBar"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    zhj:leftBtnBackground="#ff0000"
    zhj:leftBtnText="leftBtn"
    zhj:leftBtnTextColor="#000000"
    zhj:titleText="我是自定义的title"
    zhj:titleTextColor="#000000"
    zhj:titleTextSize="18sp"
    zhj:topBarBackground="#00ff00">
</com.zhuanghongji.customviewzhj.view.TopBar>

TopBar.java :


import com.zhuanghongji.customviewzhj.R;

public class TopBar extends RelativeLayout {

private Button mLeftBtn;
private TextView mTitleTv;
// 左Button属性
private int leftBtnTextColor;
private Drawable leftBtnBackground;
private String leftBtnText;
// title属性
private int titleTextColor;
private String titleText;
private float titleTextSize;
// topBar属性
private Drawable topBarBackground;
// 布局属性
private LayoutParams leftBtnParams, titleParams;
// 点击事件监听器接口 -- public
public interface TopBarClickListener {
    public void leftBtnClick();
}
private TopBarClickListener listener;
// 设置监听器
public void setOnTopBarClickListener(TopBarClickListener listener) {
    this.listener = listener;
}
// 供调用的方法
public void setLeftButtonIsVisible(boolean visible) {
    if (visible) {
        mLeftBtn.setVisibility(View.VISIBLE);
    } else {
        mLeftBtn.setVisibility(View.GONE);
    }
}
// 三个构造方法,均调用均调用三个参数的那个
public TopBar(Context context) {
    this(context, null);
}

LE);

    } else {
        mLeftBtn.setVisibility(View.GONE);
    }
}
// 三个构造方法,均调用均调用三个参数的那个
public TopBar(Context context) {
    this(context, null);
}

标签:自定义,TopBar,private,androidstudio,activity,android,zhj,public
来源: https://blog.csdn.net/m0_66155412/article/details/122650047