我有以下佈局的活動:沒有出現在 爲什麼我的動態創建按鈕(名爲btnRetry
):RelativeLayout重力不適用?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/testlayoutOverlays"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/testlayoutMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/testlayout_bottom"
android:layout_width="fill_parent"
android:layout_height="62dp"
android:background="#122334" >
<ImageView
android:id="@+id/testbtnBlock"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_gravity="left|center_vertical"
android:contentDescription="Test1"
android:padding="@dimen/padding_medium"
android:src="@drawable/btnblock" />
<TextView
android:id="@+id/testtxtZoomPan"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/testbtnX"
android:layout_toRightOf="@+id/testbtnBlock"
android:gravity="center_horizontal"
android:text="@string/txtZoomPan"
android:textColor="#FFFFFF" />
<ImageView
android:id="@+id/testbtnX"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_gravity="right|center_vertical"
android:contentDescription="Test2"
android:padding="@dimen/padding_medium"
android:src="@drawable/btnx" />
</RelativeLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/testlayoutPuzzleInfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="horizontal" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/testlayoutChronoErrors"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Chronometer
android:id="@+id/testchronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:format="@string/chronometer_initial_format"
android:gravity="center" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/testlayoutErrors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:layout_width="1px"
android:layout_height="20dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
和下面的代碼:
package minmaxdev.android.picrossanywhere;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.support.v4.app.NavUtils;
public class TestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
// Capture our button from layout
ImageView button = (ImageView) findViewById(R.id.testbtnBlock);
// Register the onClick listener with the implementation above
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
RelativeLayout rv = (RelativeLayout) findViewById(R.id.testlayoutOverlays);
rv.setGravity(Gravity.CENTER);
Button btnRetry = new Button(TestActivity.this);
btnRetry.setId(R.id.btnRetry);
btnRetry.setBackgroundResource(R.drawable.btnselector);
RelativeLayout.LayoutParams prmBtn = new RelativeLayout.LayoutParams(Util.DPsToPixels(200, getResources()), Util.DPsToPixels(40, getResources()));
prmBtn.setMargins(0, 120, 0, 0);
btnRetry.setLayoutParams(prmBtn);
// btnRetry.setGravity(Gravity.CENTER_HORIZONTAL);
btnRetry.setText("Retry");
btnRetry.setOnClickListener(new ImageView.OnClickListener() {
public void onClick(View v) {
Intent intent = getIntent();
finish();
startActivity(intent);
}
});
rv.addView(btnRetry);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_test, menu);
return true;
}
}
我想知道屏幕的中心,因爲我將父親的RelativeLayout重力設置爲rv.setGravity(Gravity.CENTER)
?
非常感謝您的時間
可能重複[Android與重力相對佈局問題](http://stackoverflow.com/questions/2804411/android-relative-layout-problem-with-gravity) –