由于android的适配问题,很多时候需要根据屏幕的宽高设置控件的宽高,就需要在java代码中进行设置,下面我就总结一下设置方法,供大家参考。</span></span>
Activity中的代码:
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final MyDialog dialog = new MyDialog(MyActivity.this, R.style.add_dialog, "mydialog");
dialog.setMargin(dip2px(MyActivity.this, 40));
dialog.show();
}
});
}
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
}
相应的xml代码,很简单不再解释:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dialog" />
</LinearLayout>
情况一.控件的代码,自定义的一个dialog:
public class MyDialog extends Dialog {
private Context mContext;
private String mText;
private LinearLayout mLL;
private int mWidth = 0;
public MyDialog(Context context, int theme, String text) {
super(context, theme);
mContext = context;
mText = text;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_layout);
TextView textView = (TextView) findViewById(R.id.dialog_tv);
Button btnCancel = (Button) findViewById(R.id.dialog_btn_cancel);
Button btnSure = (Button) findViewById(R.id.dialog_btn_sure);
textView.setText(mText);
mLL = (LinearLayout) findViewById(R.id.dialog_ll_layout);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mLL.getLayoutParams();
params.width = mContext.getResources().getDisplayMetrics().widthPixels - mWidth;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
Log.e("tag ", "width: " + params.width);
mLL.setLayoutParams(params);
btnSure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
}
public void setMargin(int width) {
mWidth = width;
Log.e("tag ", "width: " + mWidth);
}
}
上面代码中:
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mLL.getLayoutParams();
params.width = mContext.getResources().getDisplayMetrics().widthPixels - mWidth;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
mLL.setLayoutParams(params);
是设置空间宽高的代码,注意此处用的是:
LinearLayout.LayoutParams
原因是看下面xml代码中id为
dialog_ll_layout
的布局的上一层布局是LinearLayout,所以用 LinearLayout.LayoutParams,如果上一层布局是RelativeLayout,则用RelativeLayout.LayoutParams。如下面情况二部分所示。
相对应的xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/dialog_bg"
android:orientation="vertical">
<LinearLayout
android:id="@+id/dialog_ll_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="提示"
android:textColor="#2889d7"
android:gravity="center"
android:textSize="18sp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#2889d7" />
<TextView
android:id="@+id/dialog_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:padding="10dp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#cccccc" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<Button
android:id="@+id/dialog_btn_cancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="取消" />
<ImageView
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#cccccc" />
<Button
android:id="@+id/dialog_btn_sure"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="继续" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
情况二:
Activity中关键代码:
mLL = (LinearLayout) findViewById(R.id.dialog_ll_layout);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mLL.getLayoutParams();
params.width = mContext.getResources().getDisplayMetrics().widthPixels - mWidth;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
Log.e("tag ", "width: " + params.width);
mLL.setLayoutParams(params);
如果还是用情况一的LinearLayout.LayoutParams,会报错误。
xml代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/dialog_bg"
android:orientation="vertical">
<LinearLayout
android:id="@+id/dialog_ll_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="提示"
android:textColor="#2889d7"
android:gravity="center"
android:textSize="18sp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#2889d7" />
<TextView
android:id="@+id/dialog_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:padding="10dp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#cccccc" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<Button
android:id="@+id/dialog_btn_cancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="取消" />
<ImageView
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#cccccc" />
<Button
android:id="@+id/dialog_btn_sure"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="继续" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
情况三:
如果xml布局代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dialog_ll_layout"
android:background="@drawable/dialog_bg"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="提示"
android:textColor="#2889d7"
android:gravity="center"
android:textSize="18sp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#2889d7" />
<TextView
android:id="@+id/dialog_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:padding="10dp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#cccccc" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<Button
android:id="@+id/dialog_btn_cancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="取消" />
<ImageView
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#cccccc" />
<Button
android:id="@+id/dialog_btn_sure"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="继续" />
</LinearLayout>
</RelativeLayout>
上面所有控件只嵌套在一个LinearLayout下,则就不能再用LinearLayout.LayoutParams或者RelativeLayout.LayoutParams,而是用FrameLayout.LayoutParams。
总结:在线性布局或者相对布局动态设置宽高时,要先看有没有外层布局,如果有要看外层布局是什么,是LinearLayout,则要用LinearLayout.LayoutParams,如果是RelativeLayout则要用RelativeLayout.LayoutParams,而不是看你要控制的是什么布局,如果外层没有布局文件则要用FrameLayout.LayoutParams。