如何将按钮对齐到选项卡内的中心?

发布于 2024-12-12 19:54:39 字数 3147 浏览 0 评论 0原文

我正在开发一个主要由 3 个选项卡组成的程序,我遵循 本教程来创建选项卡。我遇到的问题是,当我想将按钮与选项卡的中心对齐时,它仅作为水平中心对齐(我需要它垂直和水平居中):

在此处输入图像描述

如何解决此问题?我是否以正确的方式使用选项卡?

这是我的源代码:

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>

I'm developing a program that mainly consists of 3 tabs, and I follow this tutorial to create the tabs. The problem I've faced is that when I want to align a button to the center of the tab, it is aligned only as horizontal-center (I need it to be centered both vertically and horizontally):

enter image description here

How can I fix this problem? Am I using the tabs in right way?

Here is my source code:

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

毁我热情 2024-12-19 19:54:40
<?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>

尝试使用相对布局。

<?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>

try using RelativeLayout.

红玫瑰 2024-12-19 19:54:40

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

android:layout_centerHorizo​​ntal="true"
android:layout_centerVertical="true"

你会得到你想要的。

If you switch from LinearLayout to RelativeLayout and use:

android:layout_centerHorizontal="true"
android:layout_centerVertical="true"

You will get what you want.

半边脸i 2024-12-19 19:54:40

编辑 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>

Edit the home.xml file and add the line android:gravity="center" to the LinearLayout attributes, this would center all the children of the LinearLayout. Like This:

<?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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文