Bonjour à tous,
Dans mon projet, j'ai créé une custom view pour représenter un élément que je peux appliquer un peu partout. Voici son code
et un example de layout dans lequel je le mets
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 public class Plan extends LinearLayout { @SuppressWarnings("WeakerAccess") String price = ""; @SuppressWarnings("WeakerAccess") String period = ""; @SuppressWarnings("WeakerAccess") String title = ""; @SuppressWarnings("WeakerAccess") String titleInfo = ""; public Plan(Context context) { super(context); LayoutInflater.from(context).inflate(R.layout.plan, this); } @SuppressLint("NewApi") public Plan(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.Plan, 0, 0); try { // get the text and drawables specified using the names in attrs.xml price = a.getString(R.styleable.Plan_price); period = a.getString(R.styleable.Plan_period); title = a.getString(R.styleable.Plan_plan_title); titleInfo = a.getString(R.styleable.Plan_title_info); } finally { a.recycle(); } LayoutInflater.from(context).inflate(R.layout.plan, this); TextView priceView = (TextView) this.findViewById(R.id.price); priceView.setText(price); TextView periodView = (TextView) this.findViewById(R.id.period); periodView.setText(period); TextView titleView = (TextView) this.findViewById(R.id.title); titleView.setText(title); TextView infoView = (TextView) this.findViewById(R.id.title_info); infoView.setText(titleInfo); this.setClickable(true); this.setFocusable(true); this.setFocusableInTouchMode(false); } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public String getPeriod() { return period; } public void setPeriod(String period) { this.period = period; } public void setTitle(String title) { this.title = title; } public void setTitleInfo(String titleInfo) { this.titleInfo = titleInfo; } }
Pour le projet, j'aurais besoin de modifier et/ou créer cette custom view dynamiquement. Mais je n'y arrive pas. Dans mon activité, j'ai beau mettre
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 <com.....Plan android:id="@+id/plan_hour" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="selectPrice" custom:period="@string/plan_period_hour" custom:plan_title="@string/plan_title_hour" custom:price="7 " custom:title_info="@string/plan_titleinfo_hour" />
ou via du xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 Plan planHour = new Plan(this); planHour.setPrice(rental.getPriceHour()); planHour.setPeriod(getString(R.string.plan_period_hour)); planHour.setTitle(getString(R.string.plan_title_hour)); planHour.setTitle(getString(R.string.plan_titleinfo_hour)); layout.addView(planHour);rien n'y fait. La custom view est bien dessiné (je vois le background), mais les données ne sont pas remplies.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 Plan planHour = (Plan) findViewById(R.id.plan_hour); planHour.setVisibility(View.VISIBLE); planHour.setPrice(rental.getPriceHour()); planHour.setPeriod(getString(R.string.plan_period_hour)); planHour.setTitle(getString(R.string.plan_title_hour)); planHour.setTitle(getString(R.string.plan_titleinfo_hour)); planHour.invalidate();
Comment faire ?
Merci
Partager