使用screenSlider和Fragment

时间:2019-03-11 08:19:30

标签: android android-layout android-fragments android-viewpager slider

我制作了带有片段页面的screenSlider。

问题在于,有时TextView会在单个页面中包含一个长文本,该文本不会完全显示,而只会显示第一个较短的单词(我认为这是因为其他单词会以新的形式出现在屏幕底部空白处移出一行。)

因此,我实现了TextView的自动大小调整,但是现在所有TextView看起来都很小(12sp),而且文本太短而可能在36sp正确显示了(例如只有一个小词。

MainActivity:

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扩展了FragmentStatePagerAdapter:

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter
{
    ScreenSlidePagerAdapter(FragmentManager fm)
    {
        super(fm);
    }

    @Override
    public Fragment getItem(int position)
    {
        ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();

        ...

        return fragment;
    }
}

ScreenSlidePageFragment:

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必须显示在36sp上,文本name looooooooong必须显示在< 36sp上。

谢谢

2 个答案:

答案 0 :(得分:3)

我认为您可以使用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:...

答案 1 :(得分:3)

如果Autosizing TextViews无法实现您想要的行为,则可以使用android-autofittextview库。

在了解Autosizing TextViews之前,我一直在使用它,它给了我您想要的行为。

如何做

  • 在您的gradle中:

    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"
    />