在我的主要活动中,我有一个 relativeLayout,我正在向其中动态添加一个名为 IconTray 的自定义类,Extends TableLayout
如下所示。IconTray 接受一个 ArrayList 并构建表。在表格全部建成并放置 imageViews 后,我想将表格定位为中心屏幕。我的问题是,我如何定位 IconTray,我是从主要活动还是从其本身进行。
我的主要活动:
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Point;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class HomeFavesActivity extends Activity implements OnClickListener, OnLongClickListener{
private static final String TAG = "HomeFavesCatovoty";
private ArrayList<Integer> mIcons = new ArrayList<Integer>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.v(TAG, "CREATED");
DataBaseManager db = new DataBaseManager(this);
db.getWritableDatabase();
db.AddHomeScreenIcon(1);
db.AddHomeScreenIcon(2);
db.AddHomeScreenIcon(3);
/*db.AddHomeScreenIcon(4);
db.AddHomeScreenIcon(5);
db.AddHomeScreenIcon(6);
db.AddHomeScreenIcon(7);*/
getScreenIcons(db);
}
private void getScreenIcons(DataBaseManager db){
mIcons = db.getHomeScreenIcons();
Log.v(TAG, "List Length:"+ mIcons.size());
IconTray iconTray = new IconTray(this, mIcons, null);
RelativeLayout rl = (RelativeLayout)findViewById(R.id.main);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenWidth = metrics.heightPixels;
int screenHeight =metrics.widthPixels;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(screenWidth, screenHeight);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
iconTray.setLayoutParams(params);
rl.addView(iconTray);
}
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
public boolean onLongClick(View v) {
Toast.makeText(this, "woot",
Toast.LENGTH_SHORT).show();
return true;
}
}
我的布局xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<RelativeLayout
android:id="@+id/main"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
>
</RelativeLayout>
</LinearLayout>