如何将按钮对齐到标签内的中心?

时间:2011-10-28 20:00:11

标签: java android tabs

我正在开发一个主要由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 个答案:

答案 0 :(得分: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。

答案 1 :(得分:2)

如果从LinearLayout切换到RelativeLayout并使用:

机器人:layout_centerHorizo​​ntal = “真” 机器人:layout_centerVertical = “真”

你会得到你想要的东西。

答案 2 :(得分: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>