0
votes

Comment faire Android: LinesPacingMultiplier se comporte même pour EditText et TextView

J'ai tendance à créer textview code> et editext code>, qui ressemble beaucoup à un pixel identique.

i superposition un eittext code> avec TextView code>, afin que je puisse comparer leur identique. Je fais textview code> avoir du texte de couleur rouge. P>

Voici comment ils ressemblent à ce jour. P>

edittext code> et et TextView code> identique sans Android: LinesPacingMultiplier CODE> H2>

 Entrez la description de l'image ici p>

Ceci est le XML utilisé p> xxx pré>


Toutefois, lorsqu'il est livré avec Android: LinesPacingMultiplier code>, les deux se comporte différemment. P>

editext et textview code> est différent avec Android: LunettesPacingMultiplier Code> H2>

 Entrez la description de l'image ici p>

Ceci est le XML utilisé P>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/body_text_view"
        android:gravity="top"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:layout_marginBottom="12dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        android:textSize="24sp"
        android:singleLine="false"
        android:textColor="#ff0000"
        android:lineSpacingMultiplier="2.4" />
    <EditText
        android:id="@+id/body_edit_text"
        android:gravity="top"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:layout_marginBottom="12dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        android:scrollbars="vertical"
        android:textSize="24sp"
        android:singleLine="false"
        android:inputType="textMultiLine|textCapSentences"
        android:lineSpacingMultiplier="2.4" />
</FrameLayout>


3 commentaires

Pourquoi n'utilisez-vous pas les deux eittext et faire le premier eittext android: Foncable = "FALSE"


Rendre Android: FocestABLE = "FALSE" ne fera pas eitText Comportez 100% comme TextView . Je ne me souviens pas des détails exacts. Mais croyez-moi, j'ai essayé d'innombrables temps et ça ne se comporte sûrement pas comme textview .


Le eittext est une sous-classe de textview - à l'exception de l'édition, ce qui s'applique à textview s'applique à éditext . Stackoverflow.com/q/4989545/76666442


5 Réponses :


0
votes

au lieu d'utiliser lingepacingMultiplier code>, utilisez lignesPacingextra CODE> et simplement c alcuber avec le multiplicateur fort> que vous souhaitez appliquer dans le code.

i a été capable de reproduire votre problème sur ma machine, et bien lingepacingMultiplier code> produit le même problème que vous aviez confronté et que vous ne trouviez aucune solution / raison pour laquelle cela se produirait (pas même sur les docs Android) strong>. p>

Cependant, lorsque je l'ai changé en pare-lit code> LinesPacingextra code>, par exemple: LunettesPacingextra = 20DP code>, ils ont été parfaitement alignés. strong> p>

Donc, vous pouvez calculer l'espacement souhaité que vous souhaitez pour votre editext code> et textview , et appliquez cette valeur via le code comme celui-ci: p> xxx pré>

Cette approche a fonctionné pour moi, même lorsque j'ai joué avec le Calculévalue code>, Le texte de Les deux éléments sont toujours parfaitement alignés. strong> p>

C'est le XML I ende d UP UTILISATION: P>

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:layout_width="match_parent"

    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
        android:id="@+id/body_text_view"
        android:gravity="top"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:layout_marginBottom="12dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        android:textSize="24sp"
        android:singleLine="false"
        android:textColor="#ff0000"
        android:lineSpacingExtra="0dp"/>
    <EditText
        android:id="@+id/body_edit_text"
        android:gravity="top"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:layout_marginBottom="12dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        android:scrollbars="vertical"
        android:textSize="24sp"
        android:singleLine="false"
        android:inputType="textMultiLine|textCapSentences"
        android:lineSpacingExtra="0dp"/>

</FrameLayout>


0 commentaires

0
votes
    lineSpacingExtra with 2dp worked fine for me but I was wondering what the Multiplier does instead?`<?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
        android:layout_height="match_parent">
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:lineSpacingMultiplier="1.5"
                android:text="An editable text view that shows completion suggestions ... When you define an edit text widget, you must specify the R.styleable. .... android:lineSpacingMultiplier"
                android:id="@+id/body_text_view"
                android:gravity="top"
                android:paddingLeft="16dp"
                android:paddingRight="16dp"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/transparent"
                android:textSize="24sp"
                android:singleLine="false"
                android:textColor="#ff0000" />

            <EditText
                android:lineSpacingMultiplier="1.5"
                android:text="An editable text view that shows completion suggestions ... When you define an edit text widget, you must specify the R.styleable. .... android:lineSpacingMultiplier"
                android:gravity="top"
                android:paddingLeft="16dp"
                android:paddingRight="16dp"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/transparent"
                android:textSize="24sp"
                android:singleLine="false"
                android:textColor="#000000" />


        </FrameLayout>
    </android.support.constraint.ConstraintLayout>`



2nd way Not  android:lineSpacingMultiplier=" " use LineSpacingExtra insteed   LineSpacingMultiplier

 android:lineSpacingExtra="1.5dp"



  <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:lineSpacingExtra="1.5dp"
            android:text="An editable text view that shows completion suggestions ... When you define an edit text widget, you must specify the R.styleable. .... android:lineSpacingMultiplier DD"
            android:id="@+id/body_text_view"
            android:gravity="top"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            android:textSize="24sp"
            android:singleLine="false"
            android:textColor="#ff0000" />

        <EditText
            android:lineSpacingExtra="1.5dp"
            android:text="An editable text view that shows completion suggestions ... When you define an edit text widget, you must specify the R.styleable. .... android:lineSpacingMultiplier D"
            android:gravity="top"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            android:textSize="24sp"
            android:singleLine="false"
            android:textColor="#000000" />


    </FrameLayout>
</android.support.constraint.ConstraintLayout>

0 commentaires

1
votes

Curieusement, ajouter

android:editable="true"


2 commentaires

Votre solution (paramètre Android: INPUTTYPE = "TextMultiline" ) fonctionne bien dans le réglage verticalement, mais surprenant le texte peut horizontalement ne correspond pas à : - |


Le problème était surprenant en premier lieu (en considérant un eittext est plus ou moins un textview avec un autre thème), mais c'est juste bizarre



2
votes

eittext est une extension (sous-classe) de textview , mais les deux se comportent différemment ici et que cela est certainement en raison de leurs propriétés non sincères (soit à partir de XML ou ceux non sasculaires par défaut) . Donc, la seule façon de les aligner parfaitement est de rechercher ces propriétés non sincères qui pourraient causer la question et les faire se comporter de la même manière.
Compte tenu de "Puis-je savoir, comment faire Android: LinesPacingMultiplier se comporte même pour EditText et TextView, de sorte que Editext et TextView puissent regarder visuellement identique?" comme votre principale préoccupation, J'ai été capable de les aligner parfaitement en modifiant les attributs suivants de textview .

comme indiqué dans ce document (classe TextView Classe).

Android: éditable

Si défini, spécifie que ce TextView a une méthode de saisie. Ce sera un textuel à moins d'avoir été spécifié autrement. Pour TextView, c'est faux par défaut . Pour editext, il est vrai par défaut .

Cet attribut a été obsolète mais il peut être remplacé par

Android: InputType

Le type de données placé dans un champ de texte, utilisé pour aider une méthode de saisie décider comment laisser l'utilisateur entrer du texte. Les constantes correspondent ici à celles définies par INPUTTYPE. Généralement, vous pouvez sélectionner une seule valeur, bien que certains puissent être combinés ensemble comme indiqué. Réglage de cet attribut à tout ce que aucun implique également que le texte est modifiable.

Pour le faire fonctionner efficacement utiliser TextMultiline parce que votre eittext a la même propriété.

Android: Breakstrategy

Définit la stratégie de pause pour casser des paragraphes dans des lignes. La valeur par défaut pour textview est layout.break_strategy_high_quality, et la valeur par défaut pour editext est layout.break_strategy_simple, ce dernier pour éviter le texte "danse" lors de la modification.

en un mot "/ fort>, ajout xxx

au textview les placera exactement à la même position.


0 commentaires

0
votes

Appliquez les 2 suivants dans TextView code> résout mon problème

android:breakStrategy="simple"
android:textIsSelectable="true"


0 commentaires