自定义标题栏的使用

Context context;

    private Button rightButton;
    private Button leftButton;
    private TextView titletext;

    private   OnLeftAndRightClickListener  onLeftAndRightClickListener;
    //定义一个方法
    public    void    setOnListenter( OnLeftAndRightClickListener  onLeftAndRightClickListener){
           this.onLeftAndRightClickListener=onLeftAndRightClickListener;
    }
    //自定义一个借口
    public  interface    OnLeftAndRightClickListener{
           void   LeftListenter();
           void   RightListenter();

    }
    public Top_Bars(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.top_bars,this);
        leftButton = findViewById(R.id.leftButton);
        rightButton = findViewById(R.id.rightButton);
        titletext = findViewById(R.id.titleText);
        //创建点击事件
        leftButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onLeftAndRightClickListener!=null){
                    onLeftAndRightClickListener.LeftListenter();

                }
            }
        });
        rightButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onLeftAndRightClickListener!=null){
                    onLeftAndRightClickListener.RightListenter();

                }
            }
        });
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.top_bar);
        int left_button = typedArray.getResourceId(R.styleable.top_bar_leftbutton, 0);
        int right_button = typedArray.getResourceId(R.styleable.top_bar_rightbutton, 0);
        String text_title = typedArray.getString(R.styleable.top_bar_text_title);
        float text_size = typedArray.getDimension(R.styleable.top_bar_text_size, 0);
        int text_color = typedArray.getColor(R.styleable.top_bar_text_color, 0);
        typedArray.recycle();
        leftButton.setBackgroundResource(left_button);
        rightButton.setBackgroundResource(right_button);
        titletext.setText(text_title);
        titletext.setTextSize(text_size);
        titletext.setTextColor(text_color);
    }

 

  • 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、付费专栏及课程。

余额充值