IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

Modifier background d'un button appartenant à un fragment


Sujet :

Android

  1. #1
    Membre à l'essai
    Inscrit en
    Septembre 2010
    Messages
    21
    Détails du profil
    Informations forums :
    Inscription : Septembre 2010
    Messages : 21
    Points : 16
    Points
    16
    Par défaut Modifier background d'un button appartenant à un fragment
    Bonjour à tous,

    Est-il possible de modifier le background d'un button appartenant à un fragment.

    Dans mon fichier XML main.xml j'ai :
    - une image qui s'affiche sur la totalité de mon écran (fond d'écran)
    - puis une partie supérieure de l'IHM plusieurs ImageView et Button.
    - et enfin une partie inférieure de l'IHM le fameux android.support.v4.view.ViewPager

    main.xml:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
     
    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ff0000">
        <ImageView
    		android:id="@+id/IdImgFondEcran"
    		android:src="@drawable/fond_ecran"
    		android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:layout_gravity="center"
    		/>
    		<Button
    		android:id="@+id/IdButonErase"
    		android:background="@drawable/button_erase"
    		android:onClick="fonctionErase"
    		android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:layout_marginTop="25px"
    		android:layout_marginLeft="4px"
    		/>
    	<ImageView
    		android:id="@+id/IdIconBatteryLevel"
    		android:src="@drawable/icon_battery"
    		android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:layout_marginTop="34px"
    		android:layout_marginLeft="1179px"
    		/>
             <FrameLayout android:id="@+id/IdLayoutMain" 
        	        android:layout_width="wrap_content"
        	        android:layout_height="wrap_content"
        	        android:layout_marginTop="250px">
      			<android.support.v4.view.ViewPager
      			android:id="@+android:id/viewpager"
      			android:layout_width="wrap_content"
     			android:layout_height="wrap_content"
      			/>
      	</FrameLayout>
    </FrameLayout>
    J'ai également deux fragments avec des Button
    layout_fragment1.xml (avec sa classe associée RemoteFragment1.java) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
     
    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      >
      <Button
    		android:id="@+id/IdButtonPlus"
    		android:background="@drawable/button_plus"
    		android:onClick="incremente"
    		android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:layout_marginTop="48px"
    		android:layout_marginLeft="184px"
    		/>
     
    	<Button
    		android:id="@+id/IdButtonMoins"
    		android:background="@drawable/button_moins"
    		android:onClick="defineChannel"
    		android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:layout_marginTop="158px"
    		android:layout_marginLeft="184px"
    		/>
    <FrameLayout/>
    layout_fragment2.xml (avec sa classe associée RemoteFragment2.java) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
     
    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      >
      <Button
    		android:id="@+id/IdButtonBleu"
    		android:background="@drawable/button_plus"
    		android:onClick="clicBleu"
    		android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:layout_marginTop="48px"
    		android:layout_marginLeft="184px"
    		/>
     
    	<Button
    		android:id="@+id/IdButtonRouge"
    		android:background="@drawable/button_moins"
    		android:onClick="clicRouge"
    		android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:layout_marginTop="158px"
    		android:layout_marginLeft="184px"
    		/>
    <FrameLayout/>
    J'ai également une classe PagerAdapter dérivant de FragmentPagerAdapter et enfin la classe ViewPagerFragmentActivity dérivant de FragmentActivity

    ViewPagerFragmentActivity .java :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
     
    public class ViewPagerFragmentActivity extends FragmentActivity{
     
            private static ViewPager pager;
     
    	private PagerAdapter mPagerAdapter;
     
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		super.setContentView(R.layout.main);
     
            //initialise the pager
            this.initialisePaging();
            }
     
             /**
             * Initialise the fragments to be paged
             */
    	private void initialisePaging() {
     
    		List<Fragment> fragments = new Vector<Fragment>();
    fragments.add(Fragment.instantiate(this,RemoteFragment1.class.getName()));
    		fragments.add(Fragment.instantiate(this, RemoteFragment2.class.getName()));
    		this.mPagerAdapter  = new PagerAdapter(super.getSupportFragmentManager(), fragments);
    		//
    		pager = (ViewPager)super.findViewById(R.id.viewpager);
    		pager.setAdapter(this.mPagerAdapter);
    	}
    }
    Je voudrais savoir si il est possible de changer le background du Button "IdButtonPlus" du layout_fragment1.xml. Par exemple, lors de l'affichage du fragment1, suivant la valeur d'un boolean :
    true -> android:background="@drawable/button_plus"
    false -> android:background="@drawable/button_gris"

    Est-ce possible ? Avez vous des suggestions à me faire ?

  2. #2
    Membre à l'essai
    Inscrit en
    Septembre 2010
    Messages
    21
    Détails du profil
    Informations forums :
    Inscription : Septembre 2010
    Messages : 21
    Points : 16
    Points
    16
    Par défaut
    Bonjour à tous,

    J'ai trouvé une solution :

    Dans la méthode onCreatView de mon fragment RemoteFragment1.java

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
     
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    			Bundle savedInstanceState) {
     
    	if (container == null) 
            {
                return null;
            }
     
    	FrameLayout v = (FrameLayout)inflater.inflate(R.layout.layout_fragment1, container, false);
            Button buttonPlus = (Button)v.findViewById(R.id.IdButtonPlus);
     
            if(booleanIsTrue())
          {
    buttonPlus.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_plus));
            }
            else
            {
    buttonPlus.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_gris));
            }
     
            return v;
    }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. CSS pour modifier background du LayoutUnit + PrimeFaces
    Par bruneltouopi dans le forum JSF
    Réponses: 1
    Dernier message: 19/10/2012, 20h53
  2. Impossible de modifier background-color de CalendarExtender
    Par paolo2002 dans le forum ASP.NET Ajax
    Réponses: 2
    Dernier message: 18/10/2012, 08h55
  3. modifier background-image avec lien
    Par tiesto95 dans le forum Général JavaScript
    Réponses: 8
    Dernier message: 23/02/2009, 18h40
  4. IE7 et background-image de button
    Par SpaceFrog dans le forum Mise en page CSS
    Réponses: 5
    Dernier message: 12/11/2008, 13h37
  5. [VB.NET] Modifier le background d'un TabControl
    Par exclusif dans le forum Windows Forms
    Réponses: 33
    Dernier message: 02/07/2007, 14h37

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo