MainActivity如下:
package cn.testfixmargin;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.app.Activity;
/**
* Demo描述:
* 在代码中设置布局的属性
* 比如Margin和居中
*
* 注意事项:
* 参见代码中的详细注释
*/
public class MainActivity extends Activity {
private TextView mTextView;
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
float density=displayMetrics.density;
float fontScale = displayMetrics.scaledDensity;
System.out.println("density="+density+",fontScale="+fontScale);
mTextView=(TextView) findViewById(R.id.textView);
mButton=(Button) findViewById(R.id.button);
mButton.setOnClickListener(new OnClickListenerImpl());
}
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View v) {
RelativeLayout.LayoutParams layoutParams=
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
mTextView.setLayoutParams(layoutParams);
}
}
}
package cn.testfixmargin;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.app.Activity;
/**
* Demo描述:
* 在代码中设置布局的属性
* 比如Margin和居中
*
* 注意事项:
* 参见代码中的详细注释
*/
public class MainActivity extends Activity {
private TextView mTextView;
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
float density=displayMetrics.density;
float fontScale = displayMetrics.scaledDensity;
System.out.println("density="+density+",fontScale="+fontScale);
mTextView=(TextView) findViewById(R.id.textView);
mButton=(Button) findViewById(R.id.button);
mButton.setOnClickListener(new OnClickListenerImpl());
}
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View v) {
RelativeLayout.LayoutParams layoutParams=
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
mTextView.setLayoutParams(layoutParams);
}
}
}
布局文件activity_layout.xml
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textSize="25sp"
android:layout_marginLeft="20dip"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
android:textSize="25sp"
android:layout_centerInParent="true"
/>
</RelativeLayout>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textSize="25sp"
android:layout_marginLeft="20dip"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
android:textSize="25sp"
android:layout_centerInParent="true"
/>
</RelativeLayout>