2011-10-28 34 views
0

我正在開發一個主要由3個選項卡組成的程序,我按照this tutorial來創建選項卡。我所面臨的問題是,當我想對齊按鈕的標籤的中心,它只是爲水平中心(我需要它被垂直地和水平居中)對齊:如何將按鈕對齊到標籤內部?

enter image description here

我該如何解決這個問題?我是否以正確的方式使用標籤?

這裏是我的源代碼:

MainActivity.java

public class MainActivity extends TabActivity 
{ 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Resources res = getResources(); // Resource object to get Drawables 
     TabHost tabHost = getTabHost(); // The activity TabHost 
     TabHost.TabSpec spec; // Resusable TabSpec for each tab 
     Intent intent; // Reusable Intent for each tab 

     // Create an Intent to launch an Activity for the tab (to be reused) 
     intent = new Intent().setClass(this, HomeActivity.class); 

     // Initialize a TabSpec for each tab and add it to the TabHost 
     spec = tabHost.newTabSpec("home").setIndicator("Home", 
          res.getDrawable(R.drawable.ic_tab_home)) 
         .setContent(intent); 
     tabHost.addTab(spec); 

     // Do the same for the other tabs 

     tabHost.setCurrentTab(0); 
    } 
} 


HomeActivitiy.java

public class HomeActivity extends Activity 
{ 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.home); 
    } 
} 


main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:padding="5dp"> 
     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 
     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:padding="5dp" > 
     </FrameLayout> 
    </LinearLayout> 
</TabHost> 


home.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <Button android:text="OPEN" android:id="@+id/btnOpen" 
      android:gravity="center" 
      android:layout_gravity="center" 
      android:layout_width="100dp" 
      android:layout_height="100dp"> 
    </Button> 
</LinearLayout> 

回答

3
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <Button android:text="OPEN" 
      android:id="@+id/btnOpen" 
      android:layout_centerInParent="true" 
      android:layout_width="100dp" 
      android:layout_height="100dp"> 
    </Button> 
</RelativeLayout> 

嘗試使用RelativeLayout的。

+0

+1 This works great。謝謝你的幫助 :) –

2

如果從LinearLayout中切換到RelativeLayout的用途:

機器人:layout_centerHorizo​​ntal = 「真」 機器人:layout_centerVertical = 「真」

你會得到你想要的東西。

0

編輯home.xml文件並將行android:gravity="center"添加到LinearLayout屬性中,這將使LinearLayout的所有子節點居中。像這樣:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center"> 
    <Button android:text="OPEN" android:id="@+id/btnOpen" 
      android:gravity="center" 
      android:layout_gravity="center" 
      android:layout_width="100dp" 
      android:layout_height="100dp"> 
    </Button> 
</LinearLayout>