自定义标题栏

主界面
public class MainActivity extends AppCompatActivity implements
 MyCustomActionBar.OnIconClickListener {

    private MyCustomActionBar myCustomActionBar;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myCustomActionBar = (MyCustomActionBar) findViewById(R.id.my_action_bar);
        myCustomActionBar.SetOnIconClickListener(this);
    }

    @Override
    public void OnIconClick(View icon) {
        startActivity(new Intent(this,SecondActivity.class));
    }
}
跳转到第二个界面:
public class SecondActivity extends AppCompatActivity implements MyCustomActionBar.OnIconClickListener {
    private MyCustomActionBar myCustomActionBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        myCustomActionBar = (MyCustomActionBar) findViewById(R.id.my_action_bar);
        myCustomActionBar.SetOnIconClickListener(this);
    }

    @Override
    public void OnIconClick(View icon) {
        finish();
    }
}
自定义类:
public class MyCustomActionBar extends LinearLayout {

    private RelativeLayout relativeLayout;
    private TextView biaoti;
    private ImageView img;
    private TypedArray typedArray;
    private int bgcolor;
    private int textcolor;
    private float size;
    private String text;
    private Drawable drawable;

    public MyCustomActionBar(Context context) {
        super(context);
        initView(context, null);
    }

    public MyCustomActionBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        View inflate = inflate(context, R.layout.my_action_bar_layout, this);
        relativeLayout = (RelativeLayout) inflate.findViewById(R.id.container);
        biaoti = (TextView) inflate.findViewById(R.id.biaoti);
        img = (ImageView) inflate.findViewById(R.id.img);
        img.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnIconClickListener != null){
                    mOnIconClickListener.OnIconClick(v);
                }
            }
        });
        if (attrs == null) {
            return;
        }
        initAttrs(context,attrs);
        setViewContent();
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        if (attrs == null) {
            return;
        }
     typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyCustomActionBar);
 bgcolor = typedArray.getColor(R.styleable.MyCustomActionBar_action_bar_bg_color, Color.GRAY);
 textcolor = typedArray.getColor(R.styleable.MyCustomActionBar_action_bar_title_text_color, Color.RED);
        size = typedArray.getDimension(R.styleable.MyCustomActionBar_action_bar_title_text_size, 16);
        text = typedArray.getString(R.styleable.MyCustomActionBar_action_bar_title_text);
        drawable = typedArray.getDrawable(R.styleable.MyCustomActionBar_action_bar_icon_src);
    }
    public void setViewContent(){
        relativeLayout.setBackgroundColor(bgcolor);
        img.setImageDrawable(drawable);
        biaoti.setText(text);
        biaoti.setTextColor(textcolor);
        biaoti.setTextSize(size);
    }
    private OnIconClickListener mOnIconClickListener;

    public interface OnIconClickListener {
        void OnIconClick(View icon);
    }
 public void SetOnIconClickListener(OnIconClickListener onIconClickListener) {
        mOnIconClickListener = onIconClickListener;
    }
}
attrs:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyCustomActionBar">
        <attr name="action_bar_bg_color" format="color" />
        <attr name="action_bar_title_text_color" format="color" />
        <attr name="action_bar_title_text_size" format="dimension" />
        <attr name="action_bar_title_text" format="string" />
        <attr name="action_bar_icon_src" format="reference" />
    </declare-styleable>
</resources>
自定义布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:background="@color/colorAccent">
    <ImageView
        android:layout_marginLeft="12dp"
        android:id="@+id/img"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_centerVertical="true"
        android:src="@mipmap/ic_launcher" />
    <TextView
        android:id="@+id/biaoti"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_centerInParent="true"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:src="@mipmap/ic_launcher"
        android:text="标题" />
</RelativeLayout>
主界面布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="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="com.fairy.day03.MainActivity">
<com.fairy.day03.MyCustomActionBar
    android:id="@+id/my_action_bar"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    app:action_bar_bg_color="@color/colorAccent"
    app:action_bar_icon_src="@drawable/brad_pitt"
    app:action_bar_title_text="MainActivity"
    app:action_bar_title_text_color="@color/colorPrimaryDark"
    app:action_bar_title_text_size="16dp" >
</com.fairy.day03.MyCustomActionBar>
</RelativeLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Qt 中,可以通过自定义 QWidget 的 paintEvent() 函数来实现自定义标题栏。具体步骤如下: 1.创建一个新的 QWidget,用于作为自定义标题栏。 2.将 QWidget 的窗口标志设置为 Qt::WindowFlags(Qt::FramelessWindowHint),这样可以去掉默认的标题栏。 3.重写 QWidget 的 paintEvent() 函数,在该函数中绘制自定义标题栏。 4.将 QWidget 设置为 QMainWindow 的标题栏,即调用 QMainWindow 的 setWindowTitle() 函数,并将其参数设置为自定义标题栏的指针。 下面是一个简单的示例代码: ``` #include <QtWidgets> // 继承 QWidget,作为自定义标题栏 class CustomTitleBar : public QWidget { public: CustomTitleBar(QWidget *parent = nullptr) : QWidget(parent) { // 设置窗口标志 setWindowFlags(Qt::FramelessWindowHint); } protected: void paintEvent(QPaintEvent *) override { QPainter painter(this); painter.fillRect(rect(), Qt::blue); painter.drawText(rect(), Qt::AlignCenter, "Custom Title Bar"); } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); QMainWindow mainWindow; // 设置主窗口标题 mainWindow.setWindowTitle("Main Window"); // 创建自定义标题栏 CustomTitleBar *titleBar = new CustomTitleBar(&mainWindow); // 设置自定义标题栏为主窗口的标题栏 mainWindow.setMenuWidget(titleBar); mainWindow.show(); return app.exec(); } ``` 运行该程序,可以看到自定义的蓝色标题栏。你可以根据自己的需要修改 paintEvent() 函数,来实现各种不同样式的标题栏
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值