tags:

views:

1753

answers:

3

Hi all!

I am coding a custom view, extended from RelativeLayout, and I want to resize it programmatically, How can I do?

the custom view Class is something like:

public ActiveSlideView(Context context, AttributeSet attr){
        super(context, attr);
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if(inflater != null){       
            inflater.inflate(R.layout.active_slide, this);
        }
A: 

try a this one:

...
View view = inflater.inflate(R.layout.active_slide, this);
view.setMinimumWidth(200);
zed_0xff
Sorry I haven't posted all question since you answering it. Now that is the whole question. Thanks!
herbertD
+1  A: 

and the active_slide.xml is:

<?xml version="1.0" encoding="UTF-8"?>

    <merge xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/trislide_active_layout"
        android:layout_width="fill_parent" 
        android:layout_height="120dip"      


        >
        <Gallery android:id="@+id/ondemand_gallery"

            android:layout_width="fill_parent"
            android:layout_height="100dip"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"

            android:gravity="center_vertical"
            android:spacing="10dp"
            android:paddingTop="20dp"
            android:focusable="false"           
            android:nextFocusLeft="@+id/ondemand_gallery"
            android:nextFocusRight="@+id/ondemand_gallery"
        />
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:gravity= "center"  
            android:orientation="vertical"                  
            >
            <LinearLayout
                android:layout_width = "fill_parent"
                android:layout_height = "10dip"
                />
            <ImageView android:id="@+id/ondemand_trislide_highlight_image"
                android:layout_width="60dip"
                android:layout_height="85dip"           
                android:background="@drawable/trislide_active_hl_back"
                android:paddingTop="2dip"
                android:paddingBottom="10dip"           
                android:layout_gravity="center"
            />
        </LinearLayout>
        <TextView android:id="@+id/ondemand_trislide_active_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="20dip"
            android:textColor="#FFF"
            android:textSize="15sp"
            />
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:gravity= "center"                       
            >
            <TextView android:id="@+id/ondemand_trislide_active_text_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#FFF"
                android:textSize="15sp"
                android:layout_alignParentBottom="true"

                />
            <TextView android:id="@+id/ondemand_trislide_active_text_actor"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#FFF"
                android:textSize="7sp"
                />
         </LinearLayout>
         <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:gravity= "right" 
            >

                <LinearLayout
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:gravity="right">

                <TextView android:id="@+id/ondemand_trislide_active_count"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"            

                    android:paddingTop="1dip"
                    android:paddingRight ="8dip"
                    android:textColor="#FFF"
                    android:textSize="14sp"
                    android:background="@drawable/trislide_active_item_count_back"

                />
                <LinearLayout
                    android:layout_width="10dp"
                    android:layout_height="fill_parent"/>
                </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="10dip"/>
        </LinearLayout>

    </merge>    

So that the height of this view is 120dip as I set.

Now my mission is to hide some textView and change the Relative layout height to 100dip, I'm a little confused about "measure() and onMeasure()", "setLayoutParams()", "layout()", Can you give me a best practise?

When I use the following code:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
                params.height = 130; 
                this.setLayoutParams(params);

I got a Exception in Log:

06-03 14:28:10.544: ERROR/AndroidRuntime(929): Uncaught handler: thread main exiting due to uncaught exception
06-03 14:28:10.714: ERROR/AndroidRuntime(929): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
06-03 14:28:10.714: ERROR/AndroidRuntime(929):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:326)
06-03 14:28:10.714: ERROR/AndroidRuntime(929):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
06-03 14:28:10.714: ERROR/AndroidRuntime(929):     at android.view.View.measure(View.java:7964)
06-03 14:28:10.714: ERROR/AndroidRuntime(929):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
06-03 14:28:10.714: ERROR/AndroidRuntime(929):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
06-03 14:28:10.714: ERROR/AndroidRuntime(929):     at android.view.View.measure(View.java:7964)
06-03 14:28:10.714: ERROR/AndroidRuntime(929):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:464)
06-03 14:28:10.714: ERROR/AndroidRuntime(929):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
06-03 14:28:10.714: ERROR/AndroidRuntime(929):     at android.view.View.measure(View.java:7964)
06-03 14:28:10.714: ERROR/AndroidRuntime(929):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
06-03 14:28:10.714: ERROR/AndroidRuntime(929):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
06-03 14:28:10.714: ERROR/AndroidRuntime(929):     at android.view.View.measure(View.java:7964)
06-03 14:28:10.714: ERROR/AndroidRuntime(929):     at android.view.ViewRoot.performTraversals(ViewRoot.java:763)
06-03 14:28:10.714: ERROR/AndroidRuntime(929):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
06-03 14:28:10.714: ERROR/AndroidRuntime(929):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 14:28:10.714: ERROR/AndroidRuntime(929):     at android.os.Looper.loop(Looper.java:123)
06-03 14:28:10.714: ERROR/AndroidRuntime(929):     at android.app.ActivityThread.main(ActivityThread.java:4363)
06-03 14:28:10.714: ERROR/AndroidRuntime(929):     at java.lang.reflect.Method.invokeNative(Native Method)
06-03 14:28:10.714: ERROR/AndroidRuntime(929):     at java.lang.reflect.Method.invoke(Method.java:521)
06-03 14:28:10.714: ERROR/AndroidRuntime(929):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-03 14:28:10.714: ERROR/AndroidRuntime(929):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-03 14:28:10.714: ERROR/AndroidRuntime(929):     at dalvik.system.NativeStart.main(Native Method)

Why? Should I change the inner view's size also?

Thanks in advance!

herbertD
A: 
this.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, theSizeIWant));

Problem solved!

NOTE: Be sure to use LinearLayout.LayoutParams!

herbertD