文本视图中的附加行 space
additonal line space in textview
我有一个 viewpager,里面有一个 textview。
为了填充 viewpager 的页面,我应该用 textpaint 测量 textview 行高。
当设置 viewpager 适配器时,我的问题是在 2 个不同的页面行数相等但页面 textview 高度不同并且我希望 textview 适合页面,它发生在 android 4.4.2 及以下,我怎么能解决这个问题。
请注意,我不能使用 maxLine ellipsize 或其他类似的解决方案,因为 textview 行数不确定。
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
tools:context="ru.appheads.pagesplitterapp.MainActivity">
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pages"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent">
</android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="match_parent"
android:background="#000000"
android:layout_height="1dp"></LinearLayout>
<SeekBar
android:id="@+id/pageSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="50"
android:progress="0"/>
<SeekBar
android:id="@+id/sizeSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="45"
android:progress="0"/>
<SeekBar
android:id="@+id/lineSpaceSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="40"
android:progress="0"/>
</LinearLayout>
page.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="left" >
</TextView>
页面片段:
public class PageFragment extends Fragment {
private final String PAGE_TEXT = "PAGE_TEXT";
private final String TEXT_SIZE = "TEXT_SIZE";
private final String LINE_SPACE = "LINE_SPACE";
public PageFragment newInstance(CharSequence pageText, float textSize, float lineSpace) {
PageFragment frag = new PageFragment();
Bundle args = new Bundle();
args.putCharSequence(PAGE_TEXT, pageText);
args.putFloat(TEXT_SIZE, textSize);
args.putFloat(LINE_SPACE, lineSpace);
frag.setArguments(args);
return frag;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
CharSequence text = getArguments().getCharSequence(PAGE_TEXT);
float textSize = getArguments().getFloat(TEXT_SIZE);
float lineSpace = getArguments().getFloat(LINE_SPACE);
TextView pageView = (TextView) inflater.inflate(R.layout.page, container, false);
pageView.setTextSize(TypedValue.COMPLEX_UNIT_PX, UnitConverter.spToPixels(getContext(), textSize));
pageView.setText(text);
Typeface plain = Typeface.createFromAsset(getContext().getAssets(), "Roboto_Medium.ttf");
pageView.setTypeface(plain);
pageView.setLineSpacing(UnitConverter.spToPixels(getActivity(),lineSpace), 1f);
pageView.setMovementMethod(LinkMovementMethod.getInstance());
return pageView;
}
}
您已将基本布局 LinearLayout 更改为 RelativeLayout 试试这个:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
tools:context="ru.appheads.pagesplitterapp.MainActivity">
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:layout_above="@+id/obttom_layout"
android:layout_alignParentTop="true">
</android.support.v4.view.ViewPager>
<LinearLayout
android:id="@+id/obttom_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000"></LinearLayout>
<SeekBar
android:id="@+id/pageSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="50"
android:progress="0" />
<SeekBar
android:id="@+id/sizeSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="45"
android:progress="0" />
<SeekBar
android:id="@+id/lineSpaceSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="40"
android:progress="0" />
</LinearLayout>
</RelativeLayout>
第Xml页:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pages_txt"
android:ellipsize="end"
android:gravity="left" >
</TextView>
</LinearLayout>
</ScrollView>
当然,它的设备特定问题。
为了在所有设备中获得更好的输出,您应该使用 ScrollView
作为 TextView
的容器。
另外,如果你想在文本 lines
之间添加一些 extra space
那么你可以通过编程方式使用 textView.setLineSpacing()
或者从 XML
你可以使用属性 android:lineSpacingMultiplier="1.3"
到 TextView
以获得额外的间距。
希望对你有所帮助~
我有一个 viewpager,里面有一个 textview。
为了填充 viewpager 的页面,我应该用 textpaint 测量 textview 行高。
当设置 viewpager 适配器时,我的问题是在 2 个不同的页面行数相等但页面 textview 高度不同并且我希望 textview 适合页面,它发生在 android 4.4.2 及以下,我怎么能解决这个问题。
请注意,我不能使用 maxLine ellipsize 或其他类似的解决方案,因为 textview 行数不确定。
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
tools:context="ru.appheads.pagesplitterapp.MainActivity">
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pages"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent">
</android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="match_parent"
android:background="#000000"
android:layout_height="1dp"></LinearLayout>
<SeekBar
android:id="@+id/pageSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="50"
android:progress="0"/>
<SeekBar
android:id="@+id/sizeSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="45"
android:progress="0"/>
<SeekBar
android:id="@+id/lineSpaceSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="40"
android:progress="0"/>
</LinearLayout>
page.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="left" >
</TextView>
页面片段:
public class PageFragment extends Fragment {
private final String PAGE_TEXT = "PAGE_TEXT";
private final String TEXT_SIZE = "TEXT_SIZE";
private final String LINE_SPACE = "LINE_SPACE";
public PageFragment newInstance(CharSequence pageText, float textSize, float lineSpace) {
PageFragment frag = new PageFragment();
Bundle args = new Bundle();
args.putCharSequence(PAGE_TEXT, pageText);
args.putFloat(TEXT_SIZE, textSize);
args.putFloat(LINE_SPACE, lineSpace);
frag.setArguments(args);
return frag;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
CharSequence text = getArguments().getCharSequence(PAGE_TEXT);
float textSize = getArguments().getFloat(TEXT_SIZE);
float lineSpace = getArguments().getFloat(LINE_SPACE);
TextView pageView = (TextView) inflater.inflate(R.layout.page, container, false);
pageView.setTextSize(TypedValue.COMPLEX_UNIT_PX, UnitConverter.spToPixels(getContext(), textSize));
pageView.setText(text);
Typeface plain = Typeface.createFromAsset(getContext().getAssets(), "Roboto_Medium.ttf");
pageView.setTypeface(plain);
pageView.setLineSpacing(UnitConverter.spToPixels(getActivity(),lineSpace), 1f);
pageView.setMovementMethod(LinkMovementMethod.getInstance());
return pageView;
}
}
您已将基本布局 LinearLayout 更改为 RelativeLayout 试试这个:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
tools:context="ru.appheads.pagesplitterapp.MainActivity">
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:layout_above="@+id/obttom_layout"
android:layout_alignParentTop="true">
</android.support.v4.view.ViewPager>
<LinearLayout
android:id="@+id/obttom_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000"></LinearLayout>
<SeekBar
android:id="@+id/pageSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="50"
android:progress="0" />
<SeekBar
android:id="@+id/sizeSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="45"
android:progress="0" />
<SeekBar
android:id="@+id/lineSpaceSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="40"
android:progress="0" />
</LinearLayout>
</RelativeLayout>
第Xml页:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pages_txt"
android:ellipsize="end"
android:gravity="left" >
</TextView>
</LinearLayout>
</ScrollView>
当然,它的设备特定问题。
为了在所有设备中获得更好的输出,您应该使用 ScrollView
作为 TextView
的容器。
另外,如果你想在文本 lines
之间添加一些 extra space
那么你可以通过编程方式使用 textView.setLineSpacing()
或者从 XML
你可以使用属性 android:lineSpacingMultiplier="1.3"
到 TextView
以获得额外的间距。
希望对你有所帮助~