我有一个GUI,其中最初的边距innerLayout
是0,10,0,100
,我打算hideinfoarea()
删除infoLayout
并更改其中的边距,0, 10,0,10
并showInfoarea()
应恢复初始的边距并再次显示infoLayout。
不幸的是,当我设置这些边距时,某些Layout不能很好地显示,例如标题太接近mainLayout,并且当我重新启用它时,infoLayout会覆盖一张图像。尽管在我与布局编辑器的测试中,页边距还是完美的。
此外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>
请小心,因为您可以阅读Reference:setMargins(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);
看到这个答案:从Java代码在DIP中指定尺寸的正确方法是什么?相反的:将像素转换为dp。
另外,您还可以阅读《参考》中的另一篇文章:
设置边距,以像素为单位。需要完成对requestLayout()的调用,以便考虑新的边距。
希望这可以帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句