Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在android工作室的工具栏中心图标大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我问了一个类似的问题 here
我在答案中有一些教程.
但这个问题是差异的.因为没有一个方法在我的项目中不起作用.

我想要工具栏中的所有图标

我测试所有可用的方式…但总是图标漂浮在左边.

我想要中心图标(我更喜欢左图标浮动左右图标浮动右边和其他图标浮点中心)

Activity.Main.xml

<LinearLayout xmlns:android="http://scheR_853_11845@as.android.com/apk/res/android"
    xmlns:tools="http://scheR_853_11845@as.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"   // I add vertical
    tools:context=".MainActivity">

    <include
        android:id="@+id/tool_bar"
        layout="@layout/tool_bar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"  // I add match_parent
        android:layout_gravity="center" />   // I add center
</LinearLayout>.

也在tool_bar.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    android:layout_height="wrap_content"
    android:layout_height="match_parent"
    android:BACkground="@color/ColorPriMary"
    android:elevation="2dp"
    android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"
    android:layout_gravity="center"  // I add center
    xmlns:android="http://scheR_853_11845@as.android.com/apk/res/android" />

在main_menu.xml中

<menu xmlns:android="http://scheR_853_11845@as.android.com/apk/res/android"
    xmlns:app="http://scheR_853_11845@as.android.com/apk/res-auto"
    xmlns:tools="http://scheR_853_11845@as.android.com/tools" tools:context=".MainActivity">

<item
    android:id="@+id/action_forWARD"

    android@R_615_6964@="forWARD"
    android:icon="@drawable/ic_action_arrow_forWARD"
    app:showAsAction="always"
    ></item>


<item
    android:id="@+id/action_zoom_in"
    android@R_615_6964@="zoom in"
    android:icon="@drawable/ic_action_zoom_in"
    app:showAsAction="always"
    ></item>
<item ...

对于< item>中的上述代码(main_menu.xml)我尝试了所有这代码

android:layout_width="match_parent"
android:layout_weight=".2" // 20%
android:gravity="center"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"

我真的不知道我可以做什么来对齐图标.
我上了几个小时,我尝试了所有的会议

我怎么了?
对于所有的测试我看不到任何改变…

之前我想要pic2(在上图)但现在我只想要中心!

对于我所有的测试,我只得到图1,没有任何改变.

解决方法

解决问题,享受:)
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:elevation="4dp"
    android:BACkground="?attr/colorPriMary">
    <!-- Code for your Left Button -->
    <ImageView
        android:id="@+id/yourId"
        android:src="@mipmap/yourLeftIcon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="4dp"
        android:layout_gravity="left" />
        <!-- Here is the main code for your Middle Buttons -->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:layout_gravity="center"
            android:gravity="center">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:BACkground="@drawable/ic_launcher"
                android:id="@+id/button1"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:BACkground="@drawable/ic_launcher"
                android:id="@+id/button2"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:BACkground="@drawable/ic_launcher"
                android:id="@+id/button3"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:BACkground="@drawable/ic_launcher"
                android:id="@+id/button4" />
        </LinearLayout>
    <!-- Code for your Right Button -->
    <ImageView
        android:id="@+id/yourId"
        android:src="@mipmap/yourRightIcon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="4dp"
        android:layout_gravity="right" />
</android.support.v7.widget.Toolbar>

大佬总结

以上是大佬教程为你收集整理的如何在android工作室的工具栏中心图标全部内容,希望文章能够帮你解决如何在android工作室的工具栏中心图标所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。