Java android中的一个简单TabHost 我在Android中创建了一个简单的选项卡项目
MainActivity.javaJava android中的一个简单TabHost 我在Android中创建了一个简单的选项卡项目,java,android,android-tabhost,android-button,Java,Android,Android Tabhost,Android Button,MainActivity.java public class MainActivity extends TabActivity { // TabSpec Names private static final String TAB1 = "Tab1"; private static final String TAB2 = "Tab2"; private static final String TAB3 = "Tab3";
public class MainActivity extends TabActivity {
// TabSpec Names
private static final String TAB1 = "Tab1";
private static final String TAB2 = "Tab2";
private static final String TAB3 = "Tab3";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
// Inbox Tab
TabSpec inboxSpec = tabHost.newTabSpec(TAB1);
Intent inboxIntent = new Intent(this, Tab1.class);
inboxSpec.setIndicator(TAB1);
// Tab Content
inboxSpec.setContent(inboxIntent);
// Outbox Tab
TabSpec PriceSpec = tabHost.newTabSpec(TAB2);
Intent PriceIntent = new Intent(this, Tab2.class);
PriceSpec .setIndicator(TAB2);
PriceSpec.setContent(PriceIntent);
// Profile Tab
TabSpec DistanceSpec = tabHost.newTabSpec(TAB3);
Intent DistanceIntent = new Intent(this, Tab3.class);
DistanceSpec .setIndicator(TAB3);
DistanceSpec.setContent(DistanceIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(inboxSpec);
tabHost.addTab(PriceSpec);
tabHost.addTab(DistanceSpec);
//Set the current value tab to default first tab
tabHost.setCurrentTab(0);
//Setting custom height for the tabs
final int height = 45;
tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = height;
tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = height;
tabHost.getTabWidget().getChildAt(2).getLayoutParams().height = height;
}
}
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">
<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"/>
</LinearLayout>
</TabHost>
输出:
如何在选项卡顶部制作一组水平的5个按钮,以获得下图所示的内容
我需要在
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"
android:background="@drawable/background" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include layout="@layout/screen_topbar" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/white" >
</TabWidget>
</FrameLayout>
</LinearLayout>
</TabHost>
和顶栏布局-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/buttonlayout"
android:layout_width="fill_parent"
android:layout_height="53dp"
android:gravity="top"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
试试这个,确保它能用。
和顶栏布局-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/buttonlayout"
android:layout_width="fill_parent"
android:layout_height="53dp"
android:gravity="top"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
只需尝试一下,确保它会起作用。再添加一个LinearLayout
在上面的TabWidget
中定义您的按钮,并固定您的FrameLayout
高度和重量,查看下面的代码:
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
</LinearLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/dm_footer_bg"
android:orientation="horizontal" />
<FrameLayout
android:id="@+android:id/realtabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</TabHost>
再添加一个LinearLayout
在上面的TabWidget
中定义您的按钮,并固定您的FrameLayout
高度和重量,查看下面的代码:
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
</LinearLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/dm_footer_bg"
android:orientation="horizontal" />
<FrameLayout
android:id="@+android:id/realtabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</TabHost>
试试这个。将main.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">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:layout_width="100dp" android:text=" Button 1"
android:layout_height="wrap_content" />
<Button android:layout_width="100dp" android:text=" Button 2"
android:layout_height="wrap_content" />
<Button android:layout_width="100dp" android:text=" Button 3"
android:layout_height="wrap_content" />
<Button android:layout_width="100dp" android:text=" Button 4"
android:layout_height="wrap_content" />
</LinearLayout>
<TabHost
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">
<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"/>
</LinearLayout>
</TabHost>
</LinearLayout>
试试这个。将main.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">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:layout_width="100dp" android:text=" Button 1"
android:layout_height="wrap_content" />
<Button android:layout_width="100dp" android:text=" Button 2"
android:layout_height="wrap_content" />
<Button android:layout_width="100dp" android:text=" Button 3"
android:layout_height="wrap_content" />
<Button android:layout_width="100dp" android:text=" Button 4"
android:layout_height="wrap_content" />
</LinearLayout>
<TabHost
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">
<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"/>
</LinearLayout>
</TabHost>
</LinearLayout>
您可以在选项卡栏顶部添加任何类型的线性或相对布局。我已尝试过,但无法正确设置规格。。。。你能把它编辑成你的答案吗。。。。至少有两个按钮。。。。这将有助于我更加清晰,谢谢。请检查我的答案。请放弃投票和绿色标记。感谢您的投票。您可以在选项卡栏顶部添加任何类型的线性或相对布局。我已尝试过,但无法正确设置规格。。。。你能把它编辑成你的答案吗。。。。至少有两个按钮。。。。这将有助于我更加清晰,谢谢。请检查我的答案。请放弃投票和绿色标记..谢谢你的投票..添加从一个版面到另一个版面的引用。。。。这是一个清楚明了的解释。。。。谢谢:)从一个版面添加到另一个版面的引用。。。。这是一个清楚明了的解释。。。。谢谢:)