代码中不能直接给控件设置margin属性,可以通过layoutParam来设置
这里是用ConstraintLayout布局
布局xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rootView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.qixuan.www.acf_app.forms.Login"
tools:layout_editor_absoluteY="113dp"
tools:layout_editor_absoluteX="0dp">
<TextView
android:id="@+id/lblDocNo"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="单 号:"
android:gravity="center"
tools:layout_constraintTop_creator="1"
android:layout_marginLeft="10dp"
android:layout_marginTop="33dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<EditText
android:id="@+id/txtOrderId"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:hint="请输入订单号"
android:ems="10"
android:inputType="textPersonName"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="33dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="80dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent" />
<TextView
android:id="@+id/lblCustomer"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:gravity="center"
android:text="经销商号:"
tools:layout_constraintTop_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintTop_toBottomOf="@+id/lblDocNo"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="10dp"
android:layout_marginTop="0dp"
/>
<Spinner
android:id="@+id/drpCustomers"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:ems="10"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintTop_toBottomOf="@+id/txtOrderId"
android:layout_marginTop="0dp"
android:layout_marginLeft="80dp"
android:layout_marginRight="10dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent" />
<TextView
android:id="@+id/lblProductNo"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="产品代码:"
android:gravity="center"
tools:layout_constraintTop_creator="1"
android:layout_marginLeft="10dp"
android:layout_marginTop="0dp"
app:layout_constraintTop_toBottomOf="@+id/lblCustomer"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent" />
<Spinner
android:id="@+id/drpProducts"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:ems="10"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
android:layout_marginTop="0dp"
android:layout_marginLeft="80dp"
android:layout_marginRight="10dp"
app:layout_constraintTop_toBottomOf="@+id/drpCustomers"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent" />
<TextView
android:id="@+id/lblReqQty"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:gravity="center"
android:text="计划数量:"
tools:layout_constraintTop_creator="1"
android:layout_marginLeft="10dp"
android:layout_marginTop="0dp"
app:layout_constraintTop_toBottomOf="@+id/lblProductNo"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent" />
<EditText
android:id="@+id/txtReqQty"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:hint="请输入订单数量"
android:ems="10"
android:inputType="textPersonName"
android:text=""
tools:layout_constraintTop_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintTop_toBottomOf="@+id/drpProducts"
app:layout_constraintLeft_toLeftOf="parent"
tools:layout_constraintLeft_creator="1"
android:layout_marginTop="0dp"
android:layout_marginLeft="80dp"
android:layout_marginRight="10dp" />
<TextView
android:id="@+id/lblRemarkDoc"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="备注单号:"
android:gravity="center"
tools:layout_constraintTop_creator="1"
tools:layout_constraintBottom_creator="1"
android:layout_marginLeft="10dp"
android:layout_marginTop="0dp"
app:layout_constraintTop_toBottomOf="@+id/lblReqQty"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent" />
<EditText
android:id="@+id/txtRemarkOrderId"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:ems="10"
android:hint="请输入备注单号"
android:inputType="textPersonName"
android:text=""
app:layout_constraintTop_toBottomOf="@+id/txtReqQty"
app:layout_constraintLeft_toLeftOf="parent"
tools:layout_constraintLeft_creator="1"
android:layout_marginTop="0dp"
android:layout_marginLeft="80dp"
android:layout_marginRight="10dp" />
<!--app:layout_constraintRight_toRightOf="@+id/table_title"-->
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="200dp"
android:id="@+id/horizontalScrollView"
tools:layout_constraintTop_creator="1"
tools:layout_constraintBottom_creator="1"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
app:layout_constraintTop_toBottomOf="@+id/txtRemarkOrderId"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="20dp"
app:layout_constraintLeft_toLeftOf="parent"
>
<ListView
android:id="@+id/listView_Orders"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:choiceMode="singleChoice"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1" />
</HorizontalScrollView>
<Button
android:id="@+id/btnCreate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="新建"
android:onClick=""
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toTopOf="@+id/btnSelect"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
/>
<Button
android:id="@+id/btnSelect"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="选择"
android:onClick=""
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
tools:layout_constraintBottom_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toTopOf="@+id/btnDelete"
tools:layout_constraintLeft_creator="1"
/>
<Button
android:id="@+id/btnDelete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="删除"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintBottom_toBottomOf="parent" />
</android.support.constraint.ConstraintLayout>
后台代码:
WindowManager wm = (WindowManager) this
.getSystemService(Context.WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();
//遍历界面上的控件
ConstraintLayout rootView = (ConstraintLayout)findViewById(R.id.rootView);
for (int i = 0; i < rootView.getChildCount(); i++) {
View v=rootView.getChildAt(i);
ConstraintLayout.LayoutParams layoutParam =(ConstraintLayout.LayoutParams)v.getLayoutParams();
switch(height)
{
case 800:
//指定控件类型设置
if( !(v instanceof HorizontalScrollView)&&!(v instanceof ListView))
layoutParam.height = dip2px(this, 40);
else
layoutParam.height = dip2px(this, 100);
if(i==0||i==1)//设置第一和第二个控件topMargin
layoutParam.topMargin=dip2px(this, 10);
break;
case 1520:
if( !(v instanceof HorizontalScrollView)&&!(v instanceof ListView))
layoutParam.height = dip2px(this, 50);
else
layoutParam.height = dip2px(this, 200);
if(i==0||i==1)
layoutParam.topMargin=dip2px(this, 33);
break;
default:
break;
}
}
//db转换为px
int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}