调整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);
    }
}
相关推荐
桦说编程5 小时前
Java 中如何创建不可变类型
java·后端·函数式编程
lifallen5 小时前
Java Stream sort算子实现:SortedOps
java·开发语言
IT毕设实战小研5 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
没有bug.的程序员6 小时前
JVM 总览与运行原理:深入Java虚拟机的核心引擎
java·jvm·python·虚拟机
甄超锋6 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
安卓开发者6 小时前
Android RxJava 组合操作符实战:优雅处理多数据源
android·rxjava
阿华的代码王国6 小时前
【Android】RecyclerView复用CheckBox的异常状态
android·xml·java·前端·后端
Zyy~6 小时前
《设计模式》装饰模式
java·设计模式
一条上岸小咸鱼6 小时前
Kotlin 基本数据类型(三):Booleans、Characters
android·前端·kotlin
A尘埃6 小时前
企业级Java项目和大模型结合场景(智能客服系统:电商、金融、政务、企业)
java·金融·政务·智能客服系统