I'm loosing myself in an custom size View problem since a couple of days. After some long research, I finally decide to ask your help... please ! T_T
In my app, I created a custom View with the XML calendrieradmin_day.xml :
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/CalendrierAdmin_Day_Event_TableRow"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- <TextView
android:id="@+id/CalendrierAdmin_Day_Event_TextView_Name"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="center" />-->
</TableRow>
For now, it is a simple TableRow, but I'll need to add some other things in the future. (The commented TextView, for example).
I made three classes for using it : Row , EventRow and EmptyRow . Row is abstract and EventRow and EmptyRow inherit of Row . Also, Row inherit of TableRow .
I want to add several EventRow and EmptyRow in a TableLayout and I need to resize them dynamically.
In the constructor method of EmptyRow and EventRow, I call an init() method. Here the one of EventRow :
private void init(Context c, TableLayout root) {
root = (TableLayout)LayoutInflater.from(c).inflate(R.layout.calendrieradmin_day_event, root, true);
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) root.getLayoutParams();
lp.height = getHeightOfMinute() * getDuree();
lp.width = getTheWidth();
/*
Some other stuff
...
...
*/
this.setLayoutParams(lp);
root.addView(this);
setOnClickListener(this);
setOnLongClickListener(this);
}
Of course, it doesn't work as excepted. The EventRow is not showed.
root is my TableLayout I pass in the constructor. getHeightOfMinute() and getDuree() work correctly and give me the right sizes. I used to use the inflate() method like inflate(R.layout.calendrieradmin_day_event, null, false) and it worked fine, but Eclipse gave me a warning, telling me I shouldn't pass null as the RootView argument. Although everything worked fine, I begun to search another solution and this is where I gone so far...
I used the setMinimumHeight() and setMinimumWidth() method to rezise my EventRow and EmptyRow . It works, but I think there is a better way to do that. I tried A LOT of solutions but I messed up ! I think there is something I still not catched about the functioning of inflate() and/or LayoutParams .
Could you help me ? Thanks in advance !
Please tell me if you need more piece of Source code.
PS: Sorry for the bad english, I'm french.
I finally managed it, but a lot of things have been changed.
First, I drop the TableLayout and TableRow . Now it is simply some LinearLayout .
calendrieradmin_day.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/CalendrierAdmin_Day_Event_LinearLayout_Container"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/CalendrierAdmin_Day_Event_TextView_Name"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="left"
android:background="@drawable/background_event_full"
android:gravity="left" />
</LinearLayout>
And this is the init() method :
private void init(Context c, LinearLayout root) {
LinearLayout container = (LinearLayout) LayoutInflater.from(c).inflate(R.layout.calendrieradmin_day_event, root, false);
TextView name = (TextView) container.findViewById(R.id.CalendrierAdmin_Day_Event_TextView_Name);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, 0, duree);
container.setLayoutParams(lp);
name.setLayoutParams(lptv);
name.setText(e.getNom());
root.addView(container);
name.setOnClickListener(this);
name.setOnLongClickListener(this);
}
Pay attention to the LayoutParams lp (It's a LinearLayout.LayoutParams). duree is the duration of my event, in minutes.
My EventRow are intended to be in an another LinearLayout , llEvents . It contains all the events for one day, so 1440 minutes (60 x 24). As a result, the weightSum of llEvents is 1440.
I dropped the EmptyRow and the Row class to. I just add some empty LinearLayout to llEvents for the free time schedules, with there duration as weight .
It's REALLY important to presize a "0" height in the LayoutParams
Hope It will help someone ;-) !
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.