调整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);
    }
}
相关推荐
吃海鲜的骆驼3 分钟前
服务异步通讯与RabbitMQ
java·分布式·后端·rabbitmq
m0_748233645 分钟前
RabbitMQ 进阶
android·前端·后端
羱滒6 分钟前
sql调优之数据库开发规范
java·数据库·数据库开发
Forget the Dream35 分钟前
设计模式之责任链模式
java·c++·设计模式·责任链模式
jonyleek38 分钟前
「JVS更新日志」低代码、企业会议、智能BI、智能排产2.26更新说明
java·大数据·低代码·数据分析·软件需求
计算机小白一个1 小时前
蓝桥杯 Java B 组之最短路径算法(Dijkstra、Floyd-Warshall)
java·数据结构·算法·蓝桥杯
曼岛_1 小时前
[密码学实战]Java实现SM4加解密(ecb,cbc)及工具验证
java·密码学
Forget the Dream1 小时前
设计模式之代理模式
java·c++·设计模式·代理模式
帅的飞起来2 小时前
设计模式--spring中用到的设计模式
java·spring·设计模式
張葒兵3 小时前
记一次命令行启动springboot项目的问题 java -jar的问题
java·spring boot·jar