我使用RelativeLayout创建了一个布局,但是当方向更改为横向时,我需要更改上边距。
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
RelativeLayout.LayoutParams labelLayoutParams = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
// labelLayoutParams.setMargins(0, -100, 0, 0);
layout.setLayoutParams(labelLayoutParams);
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
}
但我得到以下异常
06-25 22:22:31.166: E/AndroidRuntime(8211): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
所以请帮助如何设置布局参数特别是relativelayout的上边距? programaticaly
答案 0 :(得分:0)
试试这个:
RelativeLayout.LayoutParams labelLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
labelLayoutParams.topMargin = 10;
layout.setLayoutParams(labelLayoutParams);
答案 1 :(得分:0)
根据Set the absolute position of a view
尝试这种方式RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
params.topMargin = 60;
layout.setLayoutParams(params);