Astuces Layout et TextView

Pour effacer un écran (avant d’afficher un message ou un Toast), simplement créez un Layout vide (ici nommé « black ») et invoquez le setContentView(R.layout.black);

Taille proportionnelle du texte en fonction de l’écran : 

https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview

android:autoSizeTextType="uniform"
android:autoSizeMinTextSize="12sp"
android:autoSizeMaxTextSize="100sp"
android:autoSizeStepGranularity="2sp"

Pour souligner un texte et changer la taille de la police :

SpannableString texttimer = new SpannableString(getResources().getString(R.string.timer));
texttimer.setSpan(new UnderlineSpan(), 0, texttimer.length(), 0);
txtTimer.setText(texttimer);
txtTimer.setTextColor(getResources().getColor(R.color.white));
txtTimer.setTextSize(pxFromDp(12, MainActivity.this));

Avec

public static float pxFromDp(float dp, Context mContext) {
    return dp * mContext.getResources().getDisplayMetrics().density;
}

Pour changer la couleur des caractères dans un champ textview (avec fichier color.xml dans res/values)  :

textview.setTextColor(this.getResources().getColor(R.color.orange));


La taille des caractères : android:textSize=« 32sp »

Gras et autres attibuts : http://developer.android.com/reference/android/widget/TextView.html#attr_android%3atextSize

Pour afficher l'application en plein écran, placez dans le fichier manifest :

   
   

Pour désactiver un bouton (le griser) : myButton.setEnabled(false);

Pour centrer un bouton entre deux boutons :

In this case though, you should be able to use a simple LinearLayout with layout_weights. The layout_weight dictates how leftover space is allocated amongst your widgets. If you give one widget a value of 1, and the others 0 the one with 1 will use up all of the extra space.

<LinearLayout 
  android:layout_height="wrap_content"
  android:layout_width="fill_parent">
  <Button 
    android:id="@+id/previous_button"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_weight="0"/>
  <Button  
    android:id="@+id/menu"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
   android:layout_weight="1"/>
  <Button  
    android:id="@+id/next_button" 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_weight="0"/>

Pour un  If you wan’t to keep your RelativeLayout, just put android:layout_toRightOf= »@id/previous_button »


Faire défiler un texte :

Dans le Layout :

     

Dans l’activity :

 TextView txt = (TextView) dialog.findViewById(R.id.txt);                 
 txt.setText("Ceci est une liste de mots très très longue et encore non terminée...");  
 txt.setSelected(true); // Set focus to the textview  
 txt.setEllipsize(TruncateAt.MARQUEE);  
 txt.setSingleLine(true);  

Pour empêcher l’écran de s’éteindre :

setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

Pour ajouter des marges dans un TextView :

enter image description here

xml layout for the image above

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="#c5e1b0"
        android:textColor="#000000"
        android:text="TextView margin only"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="#f6c0c0"
        android:textColor="#000000"
        android:text="TextView margin only"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#c5e1b0"
        android:padding="10dp"
        android:textColor="#000000"
        android:text="TextView padding only"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#f6c0c0"
        android:padding="10dp"
        android:textColor="#000000"
        android:text="TextView padding only"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="#c5e1b0"
        android:textColor="#000000"
        android:padding="10dp"
        android:text="TextView padding and margin"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="#f6c0c0"
        android:textColor="#000000"
        android:padding="10dp"
        android:text="TextView padding and margin"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#c5e1b0"
        android:textColor="#000000"
        android:text="TextView no padding no margin"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#f6c0c0"
        android:textColor="#000000"
        android:text="TextView no padding no margin"
        android:textSize="20sp" />

Related

Ajouter un cadre à un TexView :

https://stackoverflow.com/questions/3496269/how-do-i-put-a-border-around-an-android-textview

Dans Res/Drawable créez un fichier « back.xml »

 <shape xmlns:android="http://schemas.android.com/apk/res/android"  
   android:shape="rectangle">  
     <!-- View background color -->  
     <solid android:color="@color/lightgray"></solid>  
     <!-- View border color and width -->  
     <stroke  
       android:width="8dp"  
       android:color="@color/colorWhite"></stroke>  
     <!-- The radius makes the corners rounded -->  
     <corners android:radius="2dp"></corners>  
 </shape>  

Et l’insérez en fond du TextView avec : android:background= »@drawable/back »

….

Votre commentaire

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:

Logo WordPress.com

Vous commentez à l’aide de votre compte WordPress.com. Déconnexion /  Changer )

Image Twitter

Vous commentez à l’aide de votre compte Twitter. Déconnexion /  Changer )

Photo Facebook

Vous commentez à l’aide de votre compte Facebook. Déconnexion /  Changer )

Connexion à %s

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur la façon dont les données de vos commentaires sont traitées.

Articles récents
Commentaires récents
fatima dans Bienvenue !
AdminDroid dans Bienvenue !
fatima dans Bienvenue !
Archives
Catégories
%d blogueurs aiment cette page :