在工具栏中均匀显示项目(图标)

时间:2016-05-03 21:37:00

标签: android toolbar

我想在工具栏中均匀显示所有五个图标。但目前工具栏的左侧只显示一个图标,工具栏内的其余空间为空。我尝试使用名为setupEvenlyDistributedToolbar的方法管理项目。

我在MainActivity中获得了此代码:

package com.example.timomichel.myapplication;

import android.content.ClipData;
import android.content.Intent;
import android.graphics.Point;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.toolbar) ;
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    setupEvenlyDistributedToolbar();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.items, menu);
    return true;
}

public void setupEvenlyDistributedToolbar() {
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.inflateMenu(R.menu.items);
    toolbar.setContentInsetsAbsolute(0, 0);

    int childCount = toolbar.getChildCount();
    int screenWidth = (width/5);

    Toolbar.LayoutParams toolbarParams = new Toolbar.LayoutParams(screenWidth, Toolbar.LayoutParams.WRAP_CONTENT);

    for (int i = 0; i < childCount; i++) {
        View childView = toolbar.getChildAt(i);
        childView.setLayoutParams(toolbarParams);
    }

}

}

我的MainActivity XML文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context="com.example.timomichel.myapplication.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />

    <include layout="@layout/toolbar" />
</RelativeLayout>

以下是工具栏的XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/black">


</android.support.v7.widget.Toolbar>

最后是菜单文件夹中项目的XML:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/profile"
    app:showAsAction="always"
    android:icon="@drawable/android_icon"
    android:title="profile" />

<item
    android:id="@+id/friends"
    app:showAsAction="always"
    android:icon="@drawable/android_icon"
    android:title="friends"/>

<item
    app:showAsAction="always"
    android:icon="@drawable/android_icon"
    android:title="maps"/>

<item
    app:showAsAction="always"
    android:icon="@drawable/android_icon"
    android:title="events" />

<item
    app:showAsAction="always"
    android:icon="@drawable/android_icon"
    android:title="Settings" />


</menu>

希望你也能帮我解决这个问题,非常感谢!

0 个答案:

没有答案