如何将按钮对齐到选项卡内的中心?
我正在开发一个主要由 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):
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用相对布局。
try using RelativeLayout.
如果您从LinearLayout切换到RelativeLayout并使用:
android:layout_centerHorizontal="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.
编辑
home.xml
文件并将行android:gravity="center"
添加到LinearLayout
属性中,这将使所有子项居中LinearLayout。像这样:Edit the
home.xml
file and add the lineandroid:gravity="center"
to theLinearLayout
attributes, this would center all the children of theLinearLayout
. Like This: