如何以编程方式在ActionBar中显示多个项目(TextView和ImageButton)?

时间:2018-01-12 10:46:13

标签: android android-actionbar layoutparams

我想在ActionBar中显示TextView和ImageButton。我能够成功渲染TextView ImageButton,但不能两者。要么我没有足够的空间来适应它们,我也不了解LayoutParams的工作方式,或者完全不了解其他内容。

只是TextView:

distance text

只是ImageButton:

Share Button

如何以编程方式在ActionBar中同时包含这两个?

MapActivity.java中的onCreate方法的声明和内部:

private ActionBarDrawerToggle mToggle;
private DrawerLayout mDrawerLayout;
private ActionBar actionBar;
private TextView actionBarDistanceText;

 /*
 ACTION BAR STUFF onCREATE
 */
    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayShowTitleEnabled(false);
    }
    mDrawerLayout = findViewById(R.id.nav_drawer);
    mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
    mDrawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    /*
    NAV STUFF onCREATE
     */
    mNavigationView = findViewById(R.id.navigation_view);
    if (mNavigationView != null) {
        mNavigationView.setNavigationItemSelectedListener(this);
    }

以下是在MapActivity.java中呈现它们的代码部分:

 /*
  CODE FOR DISTANCE TEXT AND SHARE BUTTON
 */

actionBar = getSupportActionBar();
actionBarDistanceText = new TextView(MapActivity.this);
ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(android.app.ActionBar.LayoutParams.MATCH_PARENT, android.app.ActionBar.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(150, 0, 0, 0);
actionBarDistanceText.setLayoutParams(layoutParams);
actionBarDistanceText.setTextSize(20);
actionBarDistanceText.setTextColor(Color.WHITE);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(actionBarDistanceText);

ImageButton shareBtn = new ImageButton(actionBar.getThemedContext());
shareBtn.setScaleType(ImageButton.ScaleType.CENTER);
shareBtn.setImageResource(R.drawable.share_button_selector);
shareBtn.setBackgroundColor(Color.TRANSPARENT);
ActionBar.LayoutParams shareLayoutParams = new ActionBar.LayoutParams(
            ActionBar.LayoutParams.WRAP_CONTENT,
            ActionBar.LayoutParams.WRAP_CONTENT, Gravity.END | Gravity.CENTER_VERTICAL);
shareLayoutParams.rightMargin = 40;
shareBtn.setLayoutParams(shareLayoutParams);
actionBar.setCustomView(shareBtn);

这是我的share_button_selector.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Share Button -->
    <item android:state_pressed="true" android:drawable="@drawable/ic_share_green" android:background="@null"/>
    <item android:state_pressed="false" android:drawable="@drawable/ic_share_white" android:background="@null"/>
</selector>

nav_menu.xml

 <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group
        android:id="@+id/nav_group_2">
<item android:title="@string/nav_map_type">
    <menu>
        <item
            android:id="@+id/basic_map"
            android:icon="@drawable/basic_map_24x24"

            android:title="@string/nav_basic"/>
        <item
            android:id="@+id/terrain_map"
            android:icon="@drawable/ic_terrain_black_24dp"

            android:title="@string/nav_terrain"/>
        <item
            android:id="@+id/satellite_map"
            android:icon="@drawable/satellite_24x24"

            android:title="@string/nav_sat"/>
        <item
            android:id="@+id/hybrid_map"
            android:icon="@drawable/ic_satellite_black_24dp"

            android:title="@string/nav_hybrid"/>
    </menu>
</item>
        <!--
        SETTINGS GROUP
        -->
    </group>
    <group
        android:id="@+id/nav_group_3">
        <item android:title="@string/nav_settings">
            <menu>
                <item
                    android:id="@+id/standard_distance"
                    android:icon="@drawable/ic_settings_black_24dp"
                    android:title="@string/nav_standard_setting"/>
                <item
                    android:id="@+id/metric_distance"
                    android:icon="@drawable/ic_settings_black_24dp"
                    android:title="@string/nav_metric_setting"/>
            </menu>

        </item>
    </group>

</menu>

如果我没有在MapActivity.java中注释掉ImageButton代码,TextView就会消失。只留下代码的TextView部分就可以看到它。

1 个答案:

答案 0 :(得分:1)

改为使用ToolBar

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:gravity="center"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="start|center_vertical"
            android:text="App name" />

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

</android.support.design.widget.AppBarLayout>

将标题TextView的重力设置为android:layout_gravity="center"会使标题显示在中心。对于ImageView,请使用OptionMenu