调整Android导航栏(标题栏)的宽度高度

要调整导航栏(标题栏)的宽度,可以通过修改布局文件中的样式来实现。通常,标题栏是通过一个ToolbarAppBarLayout来实现的。以下是一个示例,展示了如何调整标题栏的宽度和高度。

1. 修改布局文件

假设你的布局文件是activity_main.xml,你可以这样修改:

复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/orange"
            app:layout_scrollFlags="enterAlways|snap"
            app:title="云养Q宠"
            app:titleTextColor="@color/white"
            app:titleMarginStart="16dp"
            app:titleMarginEnd="16dp"
            app:titleMarginTop="8dp"
            app:titleMarginBottom="8dp"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </com.google.android.material.appbar.AppBarLayout>

    <!-- 其他布局内容 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintTop_toBottomOf="@id/toolbar">

        <!-- 计算器布局 -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

            <!-- 计算器标题 -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="8dp">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_timer"
                    android:contentDescription="@string/ic_timer" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="简单计算器"
                    android:textSize="24sp"
                    android:textColor="@color/black" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_dog"
                    android:contentDescription="@string/ic_dog" />

            </LinearLayout>

            <!-- 计算器输入框 -->
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="0"
                android:textSize="24sp"
                android:textColor="@color/black"
                android:padding="16dp"
                android:background="@drawable/edit_text_background" />

            <!-- 计算器按钮 -->
            <GridLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:columnCount="4"
                android:rowCount="5"
                android:padding="8dp">

                <!-- 按钮布局 -->
                <Button
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_columnWeight="1"
                    android:layout_rowWeight="1"
                    android:text="CE"
                    android:textSize="24sp"
                    android:textColor="@color/black"
                    android:background="@drawable/button_background" />

                <!-- 其他按钮 -->
                <!-- ... -->

            </GridLayout>

        </LinearLayout>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

2. 修改样式文件

确保你的样式文件(如styles.xml)中定义了相关的颜色和样式:

复制代码
<!-- styles.xml -->
<resources>
    <color name="orange">#FFD7B5</color>
    <color name="white">#FFFFFF</color>
    <color name="black">#000000</color>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
        <!-- 其他样式 -->
    </style>

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light">
        <!-- 其他样式 -->
    </style>
</resources>

3. 修改主题文件

确保你的主题文件(如themes.xml)中定义了相关的主题:

复制代码
<!-- themes.xml -->
<resources>
    <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- 其他样式 -->
    </style>
</resources>

4. 修改代码

确保你的Activity中设置了Toolbar:

复制代码
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }
}
相关推荐
李拾叁的摸鱼日常7 分钟前
Spring 框架中 RequestContextHolder 深度解析
java·架构
C++业余爱好者16 分钟前
JVM优化入门指南:JVM垃圾收集器(GC)介绍
java·开发语言·jvm
Trouvaille ~21 分钟前
【Java篇】基石与蓝图::Object 类与抽象类的双重奏
java·开发语言·javase·抽象类·类与对象·基础入门·object类
小光学长25 分钟前
基于ssm的美妆产品推荐系统rah0h134(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
java·数据库·spring
Light6033 分钟前
破局“数据孤岛”:构建业务、财务、指标三位一体的智能数据模型
java·大数据·开发语言
中文很快乐34 分钟前
从零到一:用 SpringBoot 打造 RESTful API 实战指南
java·spring boot·后端·restful
泉城老铁36 分钟前
springboot+redis 如何实现订单的过期
java·后端·架构
哈哈哈笑什么39 分钟前
在高并发分布式SpringCloud系统中,什么时候时候并行查询,提高查询接口效率,从10s到100ms
java·分布式·后端
IMPYLH40 分钟前
Lua 的 warn 函数
java·开发语言·笔记·junit·lua
泉城老铁43 分钟前
如何用Spring Boot实现分布式锁?
java·redis·后端