Android: affichage Personnalisé à partir de xml de mise en page
je voudrais créer un personnalisé View
(sous-classe de la View
classe) et d'utiliser un layout xml ressource.
Je veux quelque chose comme ceci:
public class CustomView extends LinearLayout {
public CustomView(Context context) {
super(context);
LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mInflater.inflate(R.layout.tweet, this, true);
}
Ce fait crée un View
avec le bouton droit de la hauteur et de la largeur (a ScrollView
avec une liste de ceux qui a exactement la durée prévue et les barres de défilement), mais ils sont vides (noir) même si la mise en page xml contient beaucoup de TextViews
.
C'est mon xml mise en page:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tweet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff">
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="#ffffff">
<TextView
android:text="User"
android:id="@+id/tvusername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textStyle="bold">
</TextView>
<View
android:layout_width="5dip"
android:layout_height="1dip">
</View>
<TextView
android:text="userscreenname"
android:id="@+id/tvuserscreenname"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<TextView
android:text="0s"
android:id="@+id/tvtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true">
</TextView>
</RelativeLayout>
<TextView
android:text="Tweet Content"
android:id="@+id/tvcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000000">
</TextView>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#ffffff">
</View>
<TextView
android:text="Retweet by"
android:id="@+id/tvrtby"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
C'est ma principale mise en page:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
>
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/scrollView1"
android:orientation="vertical"
android:layout_alignParentTop="true">
<LinearLayout
android:layout_width="match_parent"
android:id="@+id/timeline"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#ffffff">
<TextView
android:id="@+id/tvoutput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>
C'est de cette façon-je ajouter de la vue personnalisée à ma vue principale:
Ne fonctionne pas (ce que je cherche à atteindre):
TweetView ctweet = new TweetView(getApplicationContext(),tweet);
timeline.addView(ctweet);
Ma solution actuelle (travaux, mais n'utilise pas de vue personnalisée):
View vtweet = View.inflate(getApplicationContext(), R.layout.tweet,null);
TextView tvusername = (TextView) vtweet.findViewById(R.id.tvusername);
TextView tvuserscreenname = (TextView) vtweet.findViewById(R.id.tvuserscreenname);
TextView tvcontent = (TextView) vtweet.findViewById(R.id.tvcontent);
TextView tvtime = (TextView) vtweet.findViewById(R.id.tvtime);
tvusername.setText(tweet.getUser().getName());
tvuserscreenname.setText('@' + tweet.getUser().getScreenName());
tvcontent.setText(tweet.getText());
//tvtime.setText(ctweet.tvtime.getText());
timeline.addView(vtweet);
Posté le xml c'est la Vue personnalisée du contenu?
Oui, c'est le contenu de la ressource R. layout.customview
Et je suppose que vous gonflez la mise en page comme dans le code que vous avez posté? Utilisez-vous l'affichage personnalisé dans le fichier xml de mise en page ou de faire de l'instanciation?
Je veux instancier en appelant le constructeur et ensuite l'ajout d'un enfant à un LinearLayout dans mon activité principale.
Je ne vois pas ce qui pourrait aller mal si vous devez ajouter plus de détails, en particulier la mise en page où vous ajoutez la vue personnalisée et le code que vous utilisez pour ajouter l'affichage personnalisé.
Oui, c'est le contenu de la ressource R. layout.customview
Et je suppose que vous gonflez la mise en page comme dans le code que vous avez posté? Utilisez-vous l'affichage personnalisé dans le fichier xml de mise en page ou de faire de l'instanciation?
Je veux instancier en appelant le constructeur et ensuite l'ajout d'un enfant à un LinearLayout dans mon activité principale.
Je ne vois pas ce qui pourrait aller mal si vous devez ajouter plus de détails, en particulier la mise en page où vous ajoutez la vue personnalisée et le code que vous utilisez pour ajouter l'affichage personnalisé.
OriginalL'auteur Toast | 2012-07-17
Vous devez vous connecter pour publier un commentaire.
vous pouvez l'utiliser dans vos xml tout comme n'importe quel autre point de vue, comme
<TextView
essayer
<packageName.ClassName
ie quelque chose comme<com.example.CustomView
OriginalL'auteur Vinay Wadhwa
Probablement, vous devez définir la Mise en page Paramètres de votre TweetView objet.
OriginalL'auteur simoneL