2012-03-08 89 views
1

在我的主要活動中,我有一個relativeLayout,我在其中動態添加一個名爲IconTray的自定義類,其中Extends TableLayout在下面顯示。 IconTray需要一個ArrayList並構建表。在表格全部構建好並且imageViews已經放置好之後,我想將表格定位爲中心屏幕。我的問題是,我如何定位IconTray並從主要活動或從本身做到這一點。在RelativeLayout中定位自定義TableView

我的MainActivity:

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> 

回答

0

您可以從您HomeFavesActivity做到這一點。您在getScreenIcons方法調用rl.addView(iconTray)權利之前,添加代碼,看起來像這樣:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(someWidth, someHeight); 
params.addRule(RelativeLayout.CENTER_IN_PARENT); 
iconTray.setLayoutParams(params); 

你可能有這一點取決於你如何想要的圖標的高度和寬度玩托盤上班,但這應該讓你走上正軌。

+0

林有點困惑,因爲這似乎並不是垂直或水平居中。它似乎沿着寬度略微移動它,但它將桌子放在同一個x位置,無論我在表格中放置了多少個圖標..我編輯了上面的主要活動。 – erik 2012-03-08 20:37:08

+0

我會從設置開始您的XML文件中相對佈局的寬度設置爲「match_parent」。這將導致相對佈局的寬度填充屏幕,這反過來會導致圖標托盤至少水平居中。給一個鏡頭,讓我知道它是怎麼回事。 – harrisonlee 2012-03-08 21:29:14

+0

我最終在自定義TableLayout類中處理了它,並對其進行了更多控制。 this.setY(screenWidth/2 - TH/2 -240); this.setX(screenHeight/2 - TW/2 -40); – erik 2012-03-09 14:59:32