2017-05-13 3 views
0

J'ai un viewpager et à l'intérieur d'un textview.
Pour remplir des pages de viewpager je devrais mesurer la hauteur de la ligne de textview avec un textpaint.
Lorsque l'adaptateur viewpager est défini, mon problème est que le numéro de ligne de différence de 2 est égal mais la hauteur de la page textview est différente et je veux textview à la page, Il est arrivé dans android 4.4.2 et ci-dessous, comment puis-je résoudre problème. Notez que je ne peux pas utiliser maxLine ellipsize ou une autre solution similaire car le nombre de lignes de textview est incertain.
image 1additonal ligne espace dans textview

image2

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; 
    } 


} 

Répondre

1

Certainement, son dispositif question spécifique.

Pour une meilleure sortie dans tous les appareils, vous devez utiliser ScrollView comme conteneur de votre TextView.

En outre, si vous voulez ajouter un peu extra space entre le texte lines vous pouvez utiliser textView.setLineSpacing() programme ou de XML vous pouvez utiliser l'attribut android:lineSpacingMultiplier="1.3" à TextView pour un espacement supplémentaire.

Espérons que cela aidera ~

1

vous avez changement dans votre mise en page de base LinearLayout à RelativeLayout essayer ceci:

 <?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> 

page 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> 
+0

thx pour votre réponse, mais mon problème est que dans 2 page de différence, le numéro de ligne est égale, mais la hauteur de page est différente –

+1

ohh, il peut donc être dispositif problème spécifique, vous devez mettre votre texte de la page vue à l'intérieur de scrollview, mis à jour ma réponse, plz l'essayer une fois. –

+0

mais je ne veux pas utiliser scrollview et textview plus adapté à la page. –