Frage

I have a GUI where Initially margins of innerLayout are 0,10,0,100 and in my intention hideinfoarea() should remove the infoLayout and change the margins in 0, 10,0,10 and showInfoarea() should restore the initial margins and show infoLayout again.

Unfortunately when I set these margins some Layout become not well shown, for example the title is too close to mainLayout, and a piece of image will be covered by infoLayout when I re-enable it. despite in my test with layout editor the margins are perfect.

In adition when I invoke showInfoarea() why the layout margins aren't restored to the original conditions, if I simply reverse the operation of hideInfoarea()?

Here my code

 RelativeLayout mainLayout;
    LinearLayout innerLayout;

    public void hideInfoarea(){
        mainLayout = (RelativeLayout) findViewById(R.id.mainContainer);
        innerLayout = (LinearLayout) findViewById(R.id.innerLayout);
        mainContainer.removeView(infoLayout);

        //adjust margins
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) innerLayout.getLayoutParams();  
        params.setMargins(0, 10, 0, 10);
        innerLayout.setLayoutParams(params);
    }

public void showInfoarea(){
        mainLayout = (RelativeLayout) findViewById(R.id.mainContainer);
        innerLayout = (LinearLayout) findViewById(R.id.innerLayout);
        mainContainer.addView(infoLayout);

        //adjust margins
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) innerLayout.getLayoutParams();  
        params.setMargins(0, 10, 0, 100);
        innerLayout.setLayoutParams(params);
    }

Here the XML

   <!-- The main content view -->
    <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity"
            android:padding="5dp"
            android:gravity="fill_horizontal|fill_vertical"
            android:background="@color/Platinum"
            android:id="@+id/mainLayout">

        <TextView
                android:id="@+id/txtTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:text="Sample"
                android:textSize="12dp"
                android:layout_marginBottom="10dp"/>

        <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:id="@+id/innerlayout"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="100dp">

            <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:id="@+id/imageView"
                    android:scaleType="fitXY"
                    android:src="@drawable/rectangle"
                    android:layout_marginBottom="100dp"
                    android:layout_marginTop="10dp"/>
        </LinearLayout>

        <LinearLayout
                android:layout_width="400dp"
                android:layout_height="100dp"
                android:layout_alignParentBottom="true"
                android:background="@drawable/background_label_mm"
                android:layout_centerHorizontal="true"
                android:orientation="vertical"
                android:id="@+id/infoLayout"
                android:layout_margin="5dp">

        </LinearLayout>

    </RelativeLayout>


    <!-- The navigation drawer -->

    <ListView
            android:id="@+id/drawer"
            android:layout_width="320dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#F3F3F4"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"/>

</android.support.v4.widget.DrawerLayout>
War es hilfreich?

Lösung

Be carefull, as you can read one the Reference: setMargins(int left, int top, int right, int bottom) method use px and your layout use dp. You have to do a conversion like:

// where "dp" is your value dip
int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
                 (float) dp, getResources().getDisplayMetrics());  
// then set your margins
params.setMargins(0, px, 0, px);  

See this answer: What is the correct way to specify dimensions in DIP from Java code? and the reverse one: Converting pixels to dp.

Also, you can read another thing one the Reference:

Sets the margins, in pixels. A call to requestLayout() needs to be done so that the new margins are taken into account.

Hope this helps.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top