自定义 标题栏

先创建一个标题栏的类,需要用到 foot_bar_item.xml 和 FootBarActionParmater类。

package pageview.footBar;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.lin_ling.mynfcapplication01.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ty-deng on 17/5/6.
 */

public class FootBar extends LinearLayout {
    Context context;
    List<FootBarActionParmater> dataScoure = new ArrayList<FootBarActionParmater>();
    ActionParmaterListener listener;
    int  selectPosition = 0;

    public interface ActionParmaterListener{
        void ActionListener(int position);
    }

    public FootBar(Context context) {
        super(context);
        this.context = context;
    }

    public FootBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public FootBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    public void setFootBarActionParmater(List<FootBarActionParmater> dataScoure,ActionParmaterListener mlistener){
        this.dataScoure = dataScoure;
        this.listener = mlistener;
        setOrientation(LinearLayout.HORIZONTAL);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        int width = dm.widthPixels/dataScoure.size();
        for (int i = 0;i<dataScoure.size();i++){
            final FootBarActionParmater parmater = dataScoure.get(i);
            LayoutInflater layoutInflater = LayoutInflater.from(context);
            LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(R.layout.foot_bar_item,null);
            linearLayout.setLayoutParams(new LayoutParams(width,LayoutParams.WRAP_CONTENT));
            final ImageView imageView = (ImageView)linearLayout.findViewById(R.id.imageView1);
            imageView.setImageResource(parmater.imageNameResourceId);
            final TextView barView = (TextView) linearLayout.findViewById(R.id.textView1);
            barView.setTextColor(Color.WHITE);
            barView.setText(parmater.title);
            final int position = i;
            parmater.imageView = imageView;
            parmater.barview = barView;
            linearLayout.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view) {
                    if(listener != null){
                        selectBar(position);
                        listener.ActionListener(position);
                    }
                }
            });
            addView(linearLayout);
        }
    }

    public void selectBar(int positon){
        FootBarActionParmater parmater = dataScoure.get(selectPosition);
        parmater.imageView.setImageResource(parmater.imageNameResourceId);
        FootBarActionParmater parmaterSelect = dataScoure.get(positon);
        parmaterSelect.imageView.setImageResource(parmaterSelect.imageNameResourceIdSelected);
        selectPosition = positon;
    }

    public int  getResource(String imageName){
        int resId = getResources().getIdentifier(imageName, "drawable", context.getPackageName());
        //如果没有在"mipmap"下找到imageName,将会返回0
        return resId;
    }

}

FootBarActionParmater类

package pageview.footBar;

/**
 * Created by ty-deng on 17/5/6.
 */
public class FootBarActionParmater{
    public String title;
    public int imageNameResourceId;
    public int imageNameResourceIdSelected;
    public ImageView imageView;
    public TextView barview;

    public FootBarActionParmater(String title,int imageNameResourceId,int imageNameResourceIdSelected){
        this.title =title;
        this.imageNameResourceId = imageNameResourceId;
        this.imageNameResourceIdSelected = imageNameResourceIdSelected;
    }
}

foot_bar_item.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="wrap_content"

    android:layout_height="wrap_content">

        <ImageView
            android:layout_width="25sp"
            android:layout_height="25sp"
            android:layout_gravity="center_horizontal"
            android:paddingTop="5sp"
            android:layout_marginTop="5sp"
            android:id="@+id/imageView1" />

        <TextView
            android:text="TextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5sp"
            android:layout_gravity="center_horizontal"
            android:gravity="center"
            android:id="@+id/textView1" />

</LinearLayout>

使用布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main2"
    android:layout_width="match_parent"
    android:background="@drawable/blue_background_change"
    android:layout_height="match_parent">

    <include layout="@layout/my_title_bar"
        android:id="@+id/include" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/contectView"
        android:orientation="vertical">

        <pageview.footBar.FootBar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#fff4f4f4"
            android:id="@+id/footbar">
        </pageview.footBar.FootBar>
    </LinearLayout>
</RelativeLayout>

代码使用使用

  FootBar footBar = (FootBar) findViewById(R.id.footbar);
        List<FootBarActionParmater> dataScoure = new ArrayList<FootBarActionParmater>();
 FootBarActionParmater fooBar = new FootBarActionParmater("防伪查询",R.drawable.blue_background_change);
        dataScoure.add(fooBar);
        FootBarActionParmater fooBar1 = new FootBarActionParmater("授权查询",R.drawable.blue_background_change);
        dataScoure.add(fooBar1);
        FootBarActionParmater fooBar2 = new FootBarActionParmater("后台",R.drawable.blue_background_change);
        dataScoure.add(fooBar2);
        footBar.setFootBarActionParmater(dataScoure, new FootBar.ActionParmaterListener() {
            @Override
            public void ActionListener(int position, ImageView imageView, TextView barView) {

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

余额充值