简体   繁体   English

如何判断我的操作栏标题是否会被截断?

[英]How can I tell if my actionbar's title is going to be truncated?

In my app I want to display two pieces of information in the title of the actionbar. 在我的应用程序中,我想在操作栏的标题中显示两条信息。 However since the title can be of various length, some devices will not be able to display both pieces of information. 然而,由于标题可以具有各种长度,因此一些设备将不能显示两条信息。 If this happens then I can display the second string using getSupportActionBar().setSubtitle . 如果发生这种情况,那么我可以使用getSupportActionBar().setSubtitle显示第二个字符串。

However, the issue I'm having is that I don't know how to check if the title is going to be truncated. 但是,我遇到的问题是我不知道如何检查标题是否会被截断。 I've tried using getSupportActionBar().isTitleTruncated() , however this always returns false. 我已经尝试过使用getSupportActionBar().isTitleTruncated() ,但是这总是返回false。

        if (getSupportActionBar().isTitleTruncated())
        {
            getSupportActionBar().setTitle(username);
            getSupportActionBar().setSubtitle("❤" + " " + String.valueOf(rating));
        }
        else
        {
            getSupportActionBar().setTitle(username + "  " + "❤" + " " + String.valueOf(rating));
        }

According to this https://code.google.com/p/android/issues/detail?id=81987 isTitleTruncated() requires a layout pass in order to return. 根据此https://code.google.com/p/android/issues/detail?id=81987 isTitleTruncated()需要布局传递才能返回。 So sadly I don't think isTitleTruncated() will work for me. 很遗憾我不认为isTitleTruncated()会对我isTitleTruncated()

Does anyone know how else this can be achieved? 有谁知道如何实现这一目标?

Update: I have been playing around with DisplayMetrics and may have found a possible, albeit inconsistent solution. 更新:我一直在使用DisplayMetrics,可能已找到一个可能的,虽然不一致的解决方案。

I can get the width of a device in pixels by: 我可以通过以下方式获得设备的宽度:

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels;

And then I can get the width of the title string by: 然后我可以通过以下方式获得标题字符串的宽度:

Paint paint = new Paint();
float scaledPx = 18 * displayMetrics.density;
paint.setTextSize(scaledPx);
float stringSize = paint.measureText(username + "  " + "❤" + " " + String.valueOf(rating));

And then I if I divide the string width by the device width, I can find out what percentage of the screen width the string occupies. 然后,如果我将字符串宽度除以设备宽度,我可以找出字符串占据屏幕宽度的百分比。 With a bit of trial and error I can work out and what percentage a title truncates. 通过一些试验和错误,我可以解决标题截断的百分比。

I've so far tried this on three devices and unfortunately this number isn't very consistent (truncation has been occurring from 30% to 45% for my app). 到目前为止,我已经在三台设备上尝试了这一点,不幸的是这个数字不是很一致(我的应用程序截断率已从30%到45%)。

I am not very happy with this solution and feel like there must be more reliable methods of working this out. 我对这个解决方案不是很满意,并且觉得必须有更可靠的方法来解决这个问题。

I am having the same Issue and need a consistent library support for this problem. 我有相同的问题,需要一致的库支持这个问题。

Here is what I did: 这是我做的:

Activity.xml : Activity.xml

<android.support.design.widget.AppBarLayout>
<android.support.v7.widget.Toolbar>
//other elements
<TextView


android:id="@+id/detail_title"  
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:alpha="1.0"
android:gravity="start|center_horizontal|left"
android:text="Title"  
android:textAppearance="@style/Base.TextAppearance.AppCompat.Widget.ActionBar.Title"
// +++++++ IMPORTANT +++++++++++++
// these two lines are important. 
android:maxLines="2" // title text will expand to 2 lines.
android:lineSpacingMultiplier="0.9" // line spacing factor.
                    />

/> 
/>

and

Activity.java : Activity.java

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

// using textView in Toolbar in activity_main.xml
// disable the default title
getSupportActionBar().setDisplayShowTitleEnabled(false);
// set title using detail_text_view
detail_text_view = (TextView) findViewById(R.id.detail_title);
detail_text_view.setText("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z");

Result : 结果
在此输入图像描述

Now, If you want to change actionbar height to make more linespace, then you can try reflection to get mContentHeight property of actionbar. 现在,如果要更改操作栏高度以创建更多行空间,则可以尝试使用反射来获取操作mContentHeight属性。 and set it accordingly. 并相应地设置它。 I didn't experiment this but you should give a try. 我没试过这个,但你应该试一试。

Currently, I am trying Multiline Title bar in CollapsingToolbar using reflection as well. 目前,我正在使用反射在CollapsingToolbar中尝试多线标题栏。

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

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