简体   繁体   English

在Android中,如何在操作栏中显示用户信息?

[英]How to display user Information in an action bar, in Android?

In my app, I want to put the user profile image in an ImageView as well as the user name next to it, displayed on the right side of the action bar. 在我的应用程序中,我想将用户个人资料图像及其旁边的用户名放置在ImageView ,并显示在操作栏的右侧。

Can any one help me, how to achieve it? 谁能帮助我,如何实现呢?

What you can do is create custom layout for action bar and then assign this custom layout to action bar 您可以做的是为操作栏创建自定义布局,然后将此自定义布局分配给操作栏

 ActionBar actionBar = supportActionBar;
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#363636")));
    //   actionBar.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.bg_side_menu));
    ActionBar.LayoutParams params = new ActionBar.LayoutParams(//Center the textview in the ActionBar !
            ActionBar.LayoutParams.MATCH_PARENT,
            ActionBar.LayoutParams.MATCH_PARENT,
            Gravity.CENTER);
    View viewActionBar = ((Activity) context).getLayoutInflater().inflate(R.layout.general_top_header, null);
    actionBar.setCustomView(viewActionBar, params);

    TextView _Header_Text_View = (TextView) viewActionBar.findViewById(R.id.header_Name);
    _Header_Text_View.setText("" + header_Text);
    final ImageView back_Button = (ImageView) viewActionBar.findViewById(R.id.imageView);

    back_Button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {




        }
    });

and your general_top_header.xml will be like 和您的general_top_header.xml就像

<RelativeLayout
    android:id="@+id/Back_And_Bok_mark_button_Custome_Background"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:background="@color/top_header_color"
    android:padding="10dp"
    android:visibility="visible">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="7dp"
        android:background="@drawable/general_back_arrow_selector" />

    <TextView
        android:id="@+id/header_Name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="header"
        android:textColor="@color/white_color"
        android:textSize="20dp" />


</RelativeLayout>
<!--<View-->
    <!--android:id="@+id/toolbar_shadow"-->
    <!--android:layout_width="match_parent"-->
    <!--android:layout_height="3dp"-->
    <!--android:background="@drawable/toolbar_dropshadow" />-->

and in your style use 并以您的风格使用

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="contentInsetStart">0dp</item>
</style>

do not forgot to extend your activity with ActionBarActivity 不要忘记使用ActionBarActivity扩展您的活动

public class Your_Activity extends ActionBarActivity{
}

if you don't want to change layout for header use this. 如果您不想更改标题的布局,请使用此选项。 It is version with Universal Image Loader ( GitHub ). 它是Universal Image Loader( GitHub )的版本。 But if you don't want this lib, bottom you have another code with pure bitmap file. 但是,如果您不希望使用此库,则可以在底部使用纯位图文件编写另一个代码。

After this change your app will be look like this. 更改之后,您的应用将如下所示。

在此处输入图片说明

public void changeAvatarAndTitle(String LOGIN, String AVATARURL){
    if (android.os.Build.VERSION.SDK_INT >= 11) {
        final ActionBar actionBar = getActionBar();
        String login = LOGIN;
        String urlOfAvatr = AVATARURL;
        try {
            if (imageLoader != null)
                imageLoader.loadImage(urlOfAvatar, new SimpleImageLoadingListener() {
                    @Override
                    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                        // Do whatever you want with Bitmap
                        Bitmap b = loadedImage;
                        double density = getResources().getDisplayMetrics().density;
                        if (density >= 4.0) {
                            //"xxxhdpi";
                            b = Global.getResizedBitmap(b, 192, 192);
                        }
                        if (density >= 3.0 && density < 4.0) {
                            //xxhdpi
                            b = Global.getResizedBitmap(b, 144, 144);
                        }
                        if (density >= 2.0) {
                            //xhdpi
                            b = Global.getResizedBitmap(b, 96, 96);
                        }
                        if (density >= 1.5 && density < 2.0) {
                            //hdpi
                            b = Global.getResizedBitmap(b, 72, 72);
                        }
                        if (density >= 1.0 && density < 1.5) {
                            //mdpi
                            b = Global.getResizedBitmap(b, 48, 48);
                        }

                        b = Global.getRoundedCornerBitmap(b, 100);

                        Drawable d = new BitmapDrawable(getResources(), b);

                        if (android.os.Build.VERSION.SDK_INT > 14) {
                            actionBar.setLogo(d);
                        }
                    }
                });
            actionBar.setDisplayShowHomeEnabled(true);
            actionBar.setTitle(" " + login);
            actionBar.setDisplayUseLogoEnabled(true);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}


public static Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // CREATE A MATRIX FOR THE MANIPULATION
    Matrix matrix = new Matrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scaleWidth, scaleHeight);

    // "RECREATE" THE NEW BITMAP
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
    return resizedBitmap;
}

version without Universal Image Loader from Internet. Internet上没有Universal Image Loader的版本。

public void changeAvatarAndTitle(String LOGIN, String AVATARURL, Bitmap loadedImage){
    if (android.os.Build.VERSION.SDK_INT >= 11) {
        final ActionBar actionBar = getActionBar();
        String login = LOGIN;
        String urlOfAvatr = AVATARURL;
        Bitmap b = loadedImage;
        double density = getResources().getDisplayMetrics().density;
        if (density >= 4.0) {
            //"xxxhdpi";
            b = Global.getResizedBitmap(b, 192, 192);
        }
        if (density >= 3.0 && density < 4.0) {
            //xxhdpi
            b = Global.getResizedBitmap(b, 144, 144);
        }
        if (density >= 2.0) {
            //xhdpi
            b = Global.getResizedBitmap(b, 96, 96);
        }
        if (density >= 1.5 && density < 2.0) {
            //hdpi
            b = Global.getResizedBitmap(b, 72, 72);
        }
        if (density >= 1.0 && density < 1.5) {
            //mdpi
            b = Global.getResizedBitmap(b, 48, 48);
        }
        b = Global.getRoundedCornerBitmap(b, 100);
        Drawable d = new BitmapDrawable(getResources(), b);
        if (android.os.Build.VERSION.SDK_INT > 14) {
            actionBar.setLogo(d);
        }                   
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setTitle(" " + login);
        actionBar.setDisplayUseLogoEnabled(true);
    }
}


public static Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // CREATE A MATRIX FOR THE MANIPULATION
    Matrix matrix = new Matrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scaleWidth, scaleHeight);

    // "RECREATE" THE NEW BITMAP
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
    return resizedBitmap;
}

This can be done by simple way also.You can set the icon of 'ActionBar ' as the profile image and the title as the profile name. 也可以通过简单的方法完成。您可以将“ ActionBar”图标设置为配置文件图像,并将标题设置为配置文件名称。 However if you want more customization then using 'ToolBar' is better option. 但是,如果您想进行更多自定义,则最好使用“工具栏”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM