我制作了一个带有片段页面的屏幕滑块。
问题是在一页纸上有时
TextView
包含一个长文本,该文本不会完全显示,但只显示第一个较短的单词(我认为这是因为其他单词将在屏幕底部边距外的新行中显示)。
所以我实现了自动化
文本框
但是现在
文本框
s看起来很小(
12sp
),也可以在
36sp
(例如,只有一个小单词)。
主要活动:
public class MainActivity extends FragmentActivity
{
// The pager widget, which handles animation
// and allows swiping horizontally to access previous
// and next wizard steps.
private ViewPager mPager;
// The pager adapter, which provides the pages to the view pager widget.
private PagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
}
ScreenSlidePagerAdapter扩展碎片StatePagerAdapter:
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter
{
ScreenSlidePagerAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public Fragment getItem(int position)
{
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
...
return fragment;
}
}
public class ScreenSlidePageFragment extends Fragment
{
private int imageId;
ImageView imageView;
private String name = "";
@Override
public View onCreateView(LayoutInflater inflater
, ViewGroup container
, Bundle savedInstanceState)
{
ViewGroup rootView = null;
TextView textView = rootView.findViewById(R.id.text_view_name);
textView.setText(name);
TextViewCompat.setAutoSizeTextTypeWithDefaults(textView
, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
return rootView;
}
... getters and setters (e.g. to set the name)
}
主布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rootView">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>
寻呼机布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rootViewPager">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>
单片段页面布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/rootView">
<ImageView
android:id="@+id/image_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/text_view_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text_view_name"
android:layout_width="0dp"
android:layout_height="50dp"
android:gravity="center_vertical|center_horizontal"
android:maxLines="1"
android:text="Name of image"
app:autoSizeTextType="uniform"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/image_view" />
</android.support.constraint.ConstraintLayout>
如何在
text_view_name
在尺寸上可以统一显示的?
例如,文本
name
必须显示在
36便士
还有文字
name looooooooong
在
< 36sp
.
编辑:
我不想使用外部库。
谢谢