【问题标题】:How to display specific icons in dashboard layout?如何在仪表板布局中显示特定图标?
【发布时间】:2015-09-10 18:09:24
【问题描述】:

我正在开发一个 android 应用程序,我正在使用包含图标的仪表板布局(如图 1 所示)。仪表板布局对于所有三个选项卡都是通用的,现在我的仪表板布局如下所示,

默认情况下,当我登录我的应用程序时,邀请选项卡将打开,类似于上图。在该图像仪表板布局中有 3 个图标(即“下拉菜单、事件、设置”) 但我想根据标签点击功能更改仪表板上的图标。

例如,

在 image-2 中,“邀请选项卡”只需要在仪表板布局上显示“设置图标”,当我在“邀请选项卡”中时不需要显示其他图标。

与图 3 类似,当我在“事件选项卡”中时,我需要在仪表板布局中显示“事件和设置”图标。

在图 4 中,当我在“群聊选项卡”中时,我需要在仪表板布局中显示“下拉和设置”图标。

我的 menu_dashboard 代码如下

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:appmunu="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="ringee.app.com.ringeeapp.UserDashBoard">

    <item
        android:id="@+id/dropdown"
        android:icon="@drawable/dropdown_icon"
        android:title="Dropdown"
        appmunu:showAsAction="always">
        <menu>
            <item
                android:id="@+id/all"
                android:title="All" />
            <item
                android:id="@+id/event"
                android:title="Event" />
            <item
                android:id="@+id/invitation"
                android:title="Invitation" />
        </menu>
    </item>

我的仪表板代码如下,

<?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1" />

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="-4dp"
                android:layout_weight="0" />
        </LinearLayout>

    </android.support.v4.app.FragmentTabHost>

我的“userdashboard 活动”编程代码如下,

package com.ringee.app;



import android.app.ActionBar;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.google.gson.Gson;
import com.ringee.app.R;
import com.ringee.app.dataobjects.MessageMO;
import com.ringee.app.utility.AppActivityStatus;
import com.ringee.app.utility.Constants;

import java.util.HashSet;
import java.util.Set;
//new header file is added
import android.widget.TabHost.OnTabChangeListener;

public class UserDashBoardActivity extends ActionBarActivity {

    /** Called when the activity is first created. */
    private static final String TAB_1_TAG = "Invitation";
    private static final String TAB_2_TAG = "Event";
    private static final String TAB_3_TAG = "GroupChat";
    private FragmentTabHost tabHost;
    private Context context;
    private SharedPreferences sharedpreferences;
    private Gson gson = new Gson();

    @Override
    protected void onStart() {
        super.onStart();
        AppActivityStatus.setActivityStarted();
        AppActivityStatus.setActivityContext(context);
    }

    @Override
    protected void onPause() {
        super.onPause();
        AppActivityStatus.setActivityStoped();

    }

    @Override
    protected void onResume() {
        super.onPause();
        AppActivityStatus.setActivityStarted();
    }

    @Override
    protected void onStop() {
        super.onStop();
        AppActivityStatus.setActivityStoped();
    }

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_dash_board);
        context = getApplicationContext();
        sharedpreferences = context.getSharedPreferences(Constants.SHARED_PREFERENCE_NAME,
                Context.MODE_PRIVATE);
        // Get TabHost Refference
        tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        tabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);

        tabHost.addTab(tabHost.newTabSpec(TAB_1_TAG).setIndicator("Invitation"), InvitationFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec(TAB_2_TAG).setIndicator("Event"), OccasionFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec(TAB_3_TAG).setIndicator("GroupChat"), GroupChatFragment.class, null);

        // Set drawable images to tab
        /*tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.ic_action_event);
        tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.ic_action_phone);

        // Set Tab1 as Default tab and change image
        tabHost.getTabWidget().setCurrentTab(0);
        tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.ic_action_person);*/
        //invitation tab highlighted by default
        tabHost.getTabWidget().setCurrentTab(0);
        tabHost.getTabWidget().getChildAt(0).setBackgroundColor(getResources().getColor(R.color.Orange));
        tabHost.getTabWidget().getChildAt(1).setBackgroundColor(getResources().getColor(R.color.scandal));
        tabHost.getTabWidget().getChildAt(2).setBackgroundColor(getResources().getColor(R.color.scandal));


       /* if(getIntent().getExtras() != null && getIntent().getExtras().containsKey("messageMO")) {
            MessageMO messageMO = (MessageMO) getIntent().getExtras().get("messageMO");
            getIntent().getExtras().remove("messageMO");
            Log.i(Constants.TAG, "messageMo object" + messageMO.getMobileNumber());
            SharedPreferences.Editor editor = sharedpreferences.edit();
            editor.putString("messageMo",gson.toJson(messageMO));
            editor.commit();
           tabHost.setCurrentTab(2);
        }*/


        /*tabHost.setOnTabChangedListener(new OnTabChangeListener(){


            @Override
            public void onTabChanged(String tabId) {
                tabHost.getCurrentTabView().setBackgroundColor(getResources().getColor(R.color.scandal));

            }
        });*/
        //onTabChangedListener added for move one tab to others
        tabHost.setOnTabChangedListener(new OnTabChangeListener() {

            @Override
            public void onTabChanged(String arg0) {

                setTabColor(tabHost);
            }
        });


    }
    //setTabColor method added for highlighting the tabs
    public void setTabColor(FragmentTabHost tabHost) {

        for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
        tabHost.getTabWidget().getChildAt(i).setBackgroundColor(getResources().getColor(R.color.scandal));//unselected

        if(tabHost.getCurrentTab()==0)
            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(getResources().getColor(R.color.Orange)); //1st tab selected
        else if(tabHost.getCurrentTab()==1)
            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(getResources().getColor(R.color.Orange)); //2nd tab selected
        else if(tabHost.getCurrentTab()==2)
        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(getResources().getColor(R.color.Orange)); //3rd tab selected
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        // noinspection SimplifiableIfStatement
        if (id == R.id.account_settings) {
            Intent userSettingIntent = new Intent(getApplicationContext(),ActivityUserSettings.class);
            userSettingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(userSettingIntent);
            return true;
        }

        if (id == R.id.profile) {
            Intent profileIntent = new Intent(getApplicationContext(),ImageUploadActivity.class);
            profileIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(profileIntent);
            return true;
        }

        if(id == R.id.create_occasion){

                Intent occasionAct = new Intent(getApplicationContext(), OccasionActivity.class);
                // Clears History of Activity
                occasionAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(occasionAct);
        }
        return super.onOptionsItemSelected(item);
    }

}

如何解决这个问题,请帮助我。

【问题讨论】:

    标签: android android-layout layout


    【解决方案1】:

    在 Activity 中使用带有图标的工具栏,并在选项卡更改时自定义您的布局

    <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    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:background="@color/primary"
    android:drawingCacheQuality="low"
    android:minHeight="@dimen/abc_action_bar_default_height_material"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="wrap_content"
                  android:layout_height="match_parent"
                  android:layout_gravity="right"
                  android:layout_weight="1"
                  android:background="@android:color/transparent"
                  android:gravity="right">
    
        <ImageView
            android:id="@+id/notification"
            android:layout_width="@dimen/abc_action_button_min_width_material"
            android:layout_height="@dimen/abc_action_bar_default_height_material"
            android:layout_centerVertical="true"
            android:gravity="center"
            android:lines="1"
            android:padding="5dp"
            android:src="@drawable/ic_action_notification"
            android:textAppearance="?android:attr/textAppearanceMedium"
            />
    
        <ImageView
            android:id="@+id/searchView"
            android:layout_width="@dimen/abc_action_button_min_width_material"
            android:layout_height="@dimen/abc_action_bar_default_height_material"
            android:layout_centerVertical="true"
            android:gravity="center"
            android:lines="1"
            android:padding="5dp"
            android:src="@drawable/ic_title_search_default"
            android:textAppearance="?android:attr/textAppearanceMedium"
            />
    </LinearLayout>
    

    在你的活动上像这样

    myTabHost.setOnTabChangedListener(new OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                if (TAB_1_TAG.equals(tabId)) {
                    notification.setVisibility(View.GONE);
                    searchView.setVisibility(View.VISIBLE);
    
                }
                if (TAB_2_TAG.equals(tabId)) {
                    notification.setVisibility(View.VISIBLE);
                    searchView.setVisibility(View.GONE);
                }
            }
        });
    

    【讨论】:

      猜你喜欢
      • 2021-11-16
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-11
      • 1970-01-01
      • 2015-03-08
      相关资源
      最近更新 更多