安卓开发学习日记第一天

一、选项卡

安卓创建选项卡的四个步骤:

1、在布局文件中添加TabHost、TabWidget和TabContent组件

      整体使用TabHost包括起来,再使用垂直线性布局管理器在其中添加TabWidget组件和帧布局管理器FrameLayout

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </TabWidget>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </FrameLayout>
    </LinearLayout>
</TabHost>

2、编写各标签页的XML布局文件

  tab1.xml:线性布局管理器中添加一个ImageView组件,使用src填充图片不会进行拉伸。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:id="@+id/left"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/biaoqian_left">
    </ImageView>
</LinearLayout>

  tab2.xml:同tab1.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:id="@+id/right"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/biaoqian_right">
</ImageView>
</LinearLayout>

3、获取并初始化TabHost组件

TabHost tabHost=findViewById(android.R.id.tabhost);
 tabHost.setup();//初始化

4、为TabHost对象添加各个标签页

//使用LayoutInflater对象的inflate方法添加标签页,
        LayoutInflater layoutInflater=LayoutInflater.from(this);
        layoutInflater.inflate(R.layout.tab1,tabHost.getTabContentView());
        layoutInflater.inflate(R.layout.tab2,tabHost.getTabContentView());
        //使用TabHost对象的addTab方法加载对应的标签页
        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("精选图片").setContent(R.id.left));
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("投稿图片").setContent(R.id.right));

最终结果:

       

猜你喜欢

转载自www.cnblogs.com/liblogs/p/11447662.html