调整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);
    }
}
相关推荐
u***1377 分钟前
详解tomcat中的jmx监控
java·tomcat
Vic101018 分钟前
Java 序列化与反序列化:深入解析与实践
java·开发语言
后端小张18 分钟前
【JAVA 进阶】Spring Cloud 微服务全栈实践:从认知到落地
java·开发语言·spring boot·spring·spring cloud·微服务·原理
zl97989929 分钟前
RabbitMQ-发布确认高级
java·分布式·rabbitmq
灰小猿30 分钟前
分布式项目集成TLog实现轻量级日志链路追踪
java·分布式·springcloud·tlog·日志链路追踪
乄bluefox31 分钟前
高性能分布式 ID 生成器:基于 Redis Segment 预分配的实践
java·redis·分布式
Jiong-95236 分钟前
Java求职面试:谢飞机的奇妙旅程
java·jvm·线程池·多线程·hashmap·juc·arraylist
小二·40 分钟前
Java核心机制精讲:深入理解 static 关键字与引用传递
java·java-ee
IDOlaoluo43 分钟前
apache-tomcat-7.0.42.tar.gz 下载与安装完整教程(附安装包)
java·tomcat·apache
喝养乐多长不高1 小时前
RabbitMQ:消息确认
java·rabbitmq·java-rabbitmq