在textview中增加行距
问题描述:
我有一个viewpager,里面有一个textview。
对于填充viewpager页面,我应该使用textpaint来测量textview行高度。
当viewpager适配器设置,我的问题是,在2差异页面行数相等,但页面textview高度不同,我想textview适合页面,这是发生在Android 4.4.2和以下,我该如何解决这个问题问题。 请注意,我不能使用maxLine椭圆或其他类似的解决方案,因为textview行数不确定。 在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>
PageFragment:
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;
}
}
答
当然,它的设备特定问题。
为了在所有设备中获得更好的输出效果,您应该使用ScrollView
作为您的TextView
的容器。
另外,如果你想添加一些文字extra space
之间lines
那么你可以使用textView.setLineSpacing()
编程或XML
可以使用属性android:lineSpacingMultiplier="1.3"
到TextView
额外的间距。
希望这将有助于〜
答
你有你的基地布局的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>
THX你的答案,但我的问题是,在2差异页,行数相等,但页面高度是不同 –
喔,所以它可以是设备特定问题,你必须把你的页面文本视图滚动视图内,更新了我的答案,请尝试一次。 –
但我不想使用scrollview和textview最适合页面。 –