MainActivity以下:html
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) { // //--------如下爲測試1 在代碼中爲控件設置Margin-------- // //注意: // //1 此處的new RelativeLayout.LayoutParams(int w, int h)參數w,h指的是 // // 該控件的父控件的在佈局文件中所設置的寬和高 // //2 此處必須使用RelativeLayout.LayoutParams.FILL_PARENT() // // 由於其父類爲RelativeLayout因此是其父類的佈局參數即RelativeLayout.LayoutParams.XXX // // 注意其官方文檔的描述: // // Set the layout parameters associated with this view. // // These supply parameters to the parent of this view specifying how it should be arranged. // // 也就是說這個setLayoutParams()是給其父控件看的 // // 其實這也好理解:只有父類能夠改變子View的位置佈局.而不是說子View能夠隨意 // // 按照本身的想法擺放本身的位置,而不受父控件控制 // RelativeLayout.LayoutParams layoutParams // =new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT); // layoutParams.setMargins(280, 0, 0, 0); // mTextView.setLayoutParams(layoutParams); // //--------以上爲測試1-------- //--------如下爲測試2 在代碼中設置控件居中-------- //注意: //1 此處的new RelativeLayout.LayoutParams(int w, int h)參數w,h指的是 // 該控件在佈局文件中所設置的寬和高 //2 同測試1中的描述 RelativeLayout.LayoutParams layoutParams= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT); mTextView.setLayoutParams(layoutParams); //--------如下爲測試2-------- } } }
main.xml以下:java
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://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>