0

J'essaie de créer une disposition de bouton personnalisée. J'ai créé un fichier xml "custom_button.xml" que je gonfle ensuite dans la classe "ButtonLanguageSelection". Dans cette classe, j'ai ajouté onClickListener. J'ai ajouté cette classe à "main_activity.xml". Il se comporte comme il se doit, sauf qu'il ne recevra aucun événement tactile. Puis j'ai copié le code de "custom_button.xml" et je l'ai ajouté directement sans le gonfler, j'ai juste ajouté le paramètre android: onClick et de cette façon cela a fonctionné. Je ne peux pas savoir quel serait le problème, les dispositions sont les mêmes.xml LinearLayout comportements différents

Certains d'entre vous ont-ils des problèmes similaires? Quel pourrait être le problème? J'ai joint le code si cela aide quelqu'un à trouver le problème.

Nous vous remercions de votre aide!

custom_button.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@drawable/button_language_selection_states" 
      android:weightSum="1" 
      android:clickable="true" 
      > 

<TextView 
     android:id="@+id/textview_select_language" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_gravity="center" 
     android:gravity="center_horizontal" 
     android:paddingTop="30px" 
     android:layout_weight="0.4" 
     android:textSize="21sp" 
     android:textColor="#333333" 
/> 

<View 
     android:layout_width="match_parent" 
     android:background="@drawable/gradient" 
     android:layout_height="1dp" 
     android:layout_margin="10px"> 
</View> 

<TextView 
     android:id="@+id/textview_language" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_gravity="center" 
     android:paddingTop="40px" 
     android:layout_weight="0.6" 
     android:textSize="28sp" 
     android:textColor="#ffffff" 
/> 

</LinearLayout> 

ButtonLanguageSelection

public class ButtonLanguageSelection extends LinearLayout 
{ 

private TextView introductoryTextView; 
private TextView language; 


private final String TAG = "ButtonLanguageSelection"; 

public ButtonLanguageSelection(Context context) { 
    super(context); 
} 

public ButtonLanguageSelection(Context context, AttributeSet attrs) 
{ 
    super(context, attrs); 

    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    layoutInflater.inflate(R.layout.button_language_selection, this); 


    introductoryTextView = (TextView)findViewById(R.id.textview_select_language); 
    language = (TextView)findViewById(R.id.textview_language); 


    this.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Log.d(TAG, "onclick"); 
     } 
    }); 
} 

public ButtonLanguageSelection(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 

public void setIntroductoryTextView(String s) 
{ 
    introductoryTextView.setText(s); 
} 

public void setLanguage(String s) 
{ 
    language.setText(s); 
} 

}

main_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@drawable/screen_welcome_bg"> 

<defaultpackage.ButtonLanguageSelection 
     android:id="@+id/ButtonLanguageSelection_Slovene" 
     android:layout_width="341px" 
     android:layout_height="260px" 
     android:layout_marginRight="2px"/> 

<defaultpackage.ButtonLanguageSelection 
     android:id="@+id/ButtonLanguageSelection_Italian" 
     android:layout_width="341px" 
     android:layout_height="260px" 
     android:layout_marginRight="2px"/> 

<!--<defaultpackage.ButtonLanguageSelection--> 
     <!--android:id="@+id/ButtonLanguageSelection_English"--> 
     <!--android:layout_width="341px"--> 
     <!--android:layout_height="260px" />--> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="341px" 
       android:layout_height="260px" 
       android:background="@drawable/button_language_selection_states" 
       android:weightSum="1" 
       android:clickable="true" 
       android:onClick="sendMessage" 
     > 

    <TextView 
      android:id="@+id/textview_select_language" 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_gravity="center" 
      android:gravity="center_horizontal" 
      android:paddingTop="30px" 
      android:layout_weight="0.4" 
      android:textSize="21sp" 
      android:textColor="#333333" 
      /> 

    <View 
      android:layout_width="match_parent" 
      android:background="@drawable/gradient" 
      android:layout_height="1dp" 
      android:layout_margin="10px"> 
    </View> 

    <TextView 
      android:id="@+id/textview_language" 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_gravity="center" 
      android:paddingTop="40px" 
      android:layout_weight="0.6" 
      android:textSize="28sp" 
      android:textColor="#ffffff" 
      /> 

</LinearLayout> 

Répondre

1

Je pense que le problème est d'écrire tout le code init dans un seul constructeur. Pensez à créer une fonction init et appelez-la de tous les constructeurs. Envisagez également de définir les écouteurs d'événements onClick dans votre activité plutôt que le constructeur de mise en page lui-même.

+0

Merci pour le conseil, j'ai écrit la fonction init et je l'appelle dans tous les constructeurs. J'ai également défini l'écouteur onClick dans l'activité, mais malheureusement, le résultat est le même. – svarad

Questions connexes