2014-03-26 30 views
1

我有一个图形用户界面,其中最初的innerLayout利润率0,10,0,100和我的意图hideinfoarea()应该删除infoLayout和更改页边距在0, 10,0,10showInfoarea()应该恢复初始保证金,并再次显示infoLayout不起作用。更改页边距编程预期

不幸的是,当我设置这些边距时,某些布局变得不能很好地显示,例如标题太靠近mainLayout了,当我重新启用它时,一段图像将被infoLayout覆盖。尽管在我的布局编辑器测试中,边距是完美的。

在adition当我调用showInfoarea()为什么布局的利润率没有恢复到原来的条件,如果我只是逆转hideInfoarea()的操作?

这里我的代码

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); 
    } 

这里的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> 

回答

1

要当心,因为你可以看到一个ReferencesetMargins(int left, int top, int right, int bottom)方法使用px和布局使用dp。你必须做这样的转化:

// 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); 

看到这个答案:What is the correct way to specify dimensions in DIP from Java code?和反向之一:Converting pixels to dp

另外,可以读另外一件事一个参考:

设置页边距,以像素为单位。 需要完成对requestLayout()的调用,以便将新边距考虑在内。

希望这有助于。

+0

很高兴能帮到你,让我有一天;)愉快的编码兄弟。 – Fllo