代码之家  ›  专栏  ›  技术社区  ›  user2342558

使用ViewPager和片段自动调整TextView的大小

  •  0
  • user2342558  · 技术社区  · 6 年前

    我制作了一个带有片段页面的屏幕滑块。

    问题是在一页纸上有时 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 .

    编辑:

    我不想使用外部库。

    谢谢

    0 回复  |  直到 6 年前
        1
  •  3
  •   Tamir Abutbul AVK    6 年前

    如果 Autosizing TextViews 无法实现你想要的行为 android-autofittextview 图书馆。

    在我知道自动调整文本视图大小之前,我一直在使用它,它给了我你想要的行为。

    我们该怎么做

    • 在你的毕业典礼上:

      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"
      />
      
        2
  •  3
  •   Linh    6 年前

    我想你可以用 app:autoSizeMaxTextSize . 例子

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:autoSizeTextType="uniform"
        app:autoSizeMinTextSize="20sp"
        app:autoSizeMaxTextSize="36sp" />
    

    注意,如果您的minSDK是>=26,则可以使用 android:... 而不是 app:...