Android 使用屏幕滑块和片段自动调整TextView的大小
我用片段页面制作了一个屏幕滑块 问题是在单个页面中,有时Android 使用屏幕滑块和片段自动调整TextView的大小,android,android-layout,android-fragments,android-viewpager,slider,Android,Android Layout,Android Fragments,Android Viewpager,Slider,我用片段页面制作了一个屏幕滑块 问题是在单个页面中,有时TextView包含的长文本不会完全显示,但只会显示第一个较短的单词(我认为这是因为其他单词会在屏幕底部空白处的新行中出现) 因此,我实现了自动调整TextView大小,但现在所有TextView都显得很小(12sp),而且文本很短,可能在36sp处正确显示(例如,只有一个小字) main活动: public class MainActivity extends FragmentActivity { // The pager wid
TextView
包含的长文本不会完全显示,但只会显示第一个较短的单词(我认为这是因为其他单词会在屏幕底部空白处的新行中出现)
因此,我实现了自动调整TextView
大小,但现在所有TextView
都显得很小(12sp
),而且文本很短,可能在36sp
处正确显示(例如,只有一个小字)
main活动:
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);
}
}
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>
ScreenSlidePagerAdapter扩展了FragmentStatePagerAdapter:
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);
}
}
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>
屏幕幻灯片片段:
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);
}
}
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>
主布局:
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);
}
}
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>
寻呼机布局:
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);
}
}
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>
单片段页面布局:
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);
}
}
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\u view\u name
中的文本在大小上能够一致显示
例如,文本name
必须显示在36sp
处,文本name loooooong
必须显示在<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);
}
}
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>
我不想使用外部库
谢谢我想你可以使用
应用程序:autosizemaxtsize
。范例
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
app:autoSizeTextType="uniform"
app:autoSizeMinTextSize="20sp"
app:autoSizeMaxTextSize="36sp" />
请注意,如果您的minSDK>=26,您可以使用android:…而不是app:…如果无法实现您想要的行为,您可以使用library
在我了解自动调整文本视图大小之前,我一直在使用它,它给了我你想要的行为
如何使用
- 在你的毕业典礼上:
dependencies { compile 'me.grantland:autofittextview:0.2.1' }
- 在XML中:
<me.grantland.widget.AutofitTextView android:layout_width="match_parent" android:layout_height="wrap_content" android:singleLine="true" android:maxLines="2" android:textSize="36sp" autofit:minTextSize="12sp" />
app:autoSizeMinTextSize=“26dp”
。这将有助于添加文本外观的图像。还有一件事-我一直在使用这个库,它工作得很好,一点也不会影响你的性能-我真的很喜欢这个库。你能给我一个使用自动调整文本视图大小的代码示例吗?