调整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);
    }
}
相关推荐
二十七剑1 小时前
jvm中各个参数的理解
java·jvm
东阳马生架构2 小时前
JUC并发—9.并发安全集合四
java·juc并发·并发安全的集合
计算机小白一个3 小时前
蓝桥杯 Java B 组之岛屿数量、二叉树路径和(区分DFS与回溯)
java·数据结构·算法·蓝桥杯
菠菠萝宝3 小时前
【Java八股文】10-数据结构与算法面试篇
java·开发语言·面试·红黑树·跳表·排序·lru
openinstall全渠道统计3 小时前
免填邀请码工具:赋能六大核心场景,重构App增长新模型
android·ios·harmonyos
不会Hello World的小苗3 小时前
Java——链表(LinkedList)
java·开发语言·链表
双鱼大猫3 小时前
一句话说透Android里面的ServiceManager的注册服务
android
双鱼大猫3 小时前
一句话说透Android里面的查找服务
android
双鱼大猫3 小时前
一句话说透Android里面的SystemServer进程的作用
android
双鱼大猫3 小时前
一句话说透Android里面的View的绘制流程和实现原理
android