The title content may be a bit confusing ~ Here I will explain that this is actually the case.
PagerView is an official control that can display sliding screen effects.
The layout file code is
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
It also provides a title layout,
<android.support.v4.view.PagerTitleStrip android:id="@+id/pager_title_strip" android:layout_width="wrap_content" android:layout_height="wrap_content"
However, this title has a restriction, that is, it cannot be displayed in multiple rows. If you want to make something similar to a questionnaire that has a sliding screen effect, the title can be considered a questionnaire, in this case, all titles must be displayed, so you can naturally think of using TextView to replace the official PagerTitleStrip. Therefore, the problem can be easily solved by adding
android:singleLine="false"
This attribute enables the title to wrap automatically. It seems that the problem has been solved. Of course, if this is the case, it would be good, at least simply and easily.
However, if the following requirements are met, the page and page will be more beautiful when the screen is sliding (think about it, if the title content on the first page can be automatically changed to three lines, the content on the second page is automatically changed to two rows. In this case, the content under the title between the page and the page is not in a horizontal line ), so should we use the title of each page to have the same height?
Indeed, this is what I want to explain in the title. However, I believe that very few people will encounter such a situation. If you do, you can consider using the following methods.
First, I thought of dynamically retrieving the height after the line feed of each page title in the code, that is
int currentHeight = tvTitle.getLineCount() * tvTitle.getLineHeight();
In this way, a piece of code is implemented, and then the title height of each page is traversed, and the highest value is obtained.
tvTitle.setHeight(maxHeight);
Set it.
But there are two problems in this process: 1. Because the creation of the entire view is implemented in the onCreateView () method of the Fragment class, tvTitle. getLineCount () is always 0, because this method is used to obtain the real value only after the view is successfully created. Otherwise, 0 is returned, which is an insurmountable gap.
But there is another problem. 2. I mentioned that traversing the height of each page is really awkward, but I thought it was okay, if you want to view the view life cycle in the Fragment class or add some print information to onCreateView (), you will find that unless you slide the page by yourself, otherwise, the system will not "Touch" those pages that are not rendered, so this method is not feasible.
But the problem must be solved and never solved. So the second method appears.
Because the title content is known, we can calculate the total pixel size of each page title (pixels), select the largest one, and then calculate the pixel size displayed in a row of TextView, the two are divided to determine the number of rows. I think I can only do this now.
Because the title content may contain Chinese characters, characters, numbers, and other symbols, we must take them into consideration. A function is used here.
// Calculate the String pixel size. public int getPixels (String parseStr) {if (parseStr = null | parseStr. length () <= 0) {return 0;} int pixelsLength = 0; char c; for (int I = parseStr. length ()-1; I> = 0; I --) {c = parseStr. charAt (I); if (c> = '0' & c <= '9 ') | (c> = 'A' & c <= 'Z ')) {// letter, number pixelsLength ++;} else {if (Character. isLetter (c) {// Chinese pixelsLength + = 2;} else {// symbol or control character pixelsLength ++ ;}} pixelsLength = (int) pixelsLength/2; return pixelsLength ;}
There is a piece of pixelsLength/2 later, because I found that after the calculated length/2, it is the pixel value of the real TextView line. I have no strength to study it in depth, I thought it was necessary to add the spacing or something, but after actual operations, I found that they were redundant.
So follow my methods.
Try {// get the pixel size (pixels) of the TextView, that is, the size of all pixels in a single row. int titleWidth = screenWidth-tvTitle. getPaddingLeft ()-tvTitle. getPaddingRight (); // Number of pixel units that can be placed in a single row int singleLineLength = (int) (titleWidth/(int) tvTitle. getTextSize (); // total pixel size/single row pixel size = required number of rows int lineNum = titleLength/singleLineLength; if (lineNum = 0) {lineNum = 1 ;} if (lineNum! = 0) {lineNum ++;} // sets the height of TextView tvTitle. setHeight (lineNum * tvTitle. getLineHeight () + 5);} catch (Exception e) {e. printStackTrace ();}
In addition, this section has brought this problem to an end for the time being.
This article is from the "typical lion man" blog, please be sure to keep this source http://zhouhongyu1989.blog.51cto.com/2931598/1285357