簡體   English   中英

Android:如何在Tab Host中添加圖像

[英]Android : How to add images in Tab Host

我是android編程的新手。 我在活動中創建了一個Tab Host。 (以下截圖) 截圖

我想在這些標簽的每個標簽中添加圖像:

_個人資料

_歷史

_Visits

我怎樣才能做到這一點?

對不起,英語不好...謝謝。

這是我在activity_main.xml中的代碼

<LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="center"
            android:background="#FFFFFF"
            android:paddingTop="55dp">

                <TabHost
                    android:id="@+id/tabHost"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true">

                    <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="match_parent"
                            android:layout_height="match_parent">

                            <LinearLayout
                                android:id="@+id/PersonalData_Tab"
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:orientation="vertical"
                                android:background="#FFFFFF">

                                <ImageView
                                    android:id="@+id/IMG_User"
                                    android:layout_width="105dp"
                                    android:layout_height="105dp"
                                    android:src="@drawable/img_user"
                                    android:clickable="true"
                                    android:layout_gravity="center_horizontal"
                                    style="@style/myImageView"
                                    android:scaleType="fitXY" />

                                <TextView
                                    android:id="@+id/ET_RecordNumber"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_gravity="center_horizontal"
                                    android:hint="Record Number"
                                    android:ems="10"
                                    android:singleLine="true"
                                    android:visibility="invisible"
                                    android:paddingTop="10dp"/>

                                <EditText
                                    android:id="@+id/ET_Name"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_gravity="center_horizontal"
                                    android:hint="Your Name"
                                    android:ems="10"
                                    android:singleLine="true"
                                    android:paddingTop="20dp" />

                                <EditText
                                    android:id="@+id/ET_Age"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_gravity="center_horizontal"
                                    android:hint="Your Age"
                                    android:inputType="date"
                                    android:ems="10"
                                    android:singleLine="true"
                                    android:paddingTop="25dp" />

                                <EditText
                                    android:id="@+id/ET_Job"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_gravity="center_horizontal"
                                    android:hint="Your Job"
                                    android:ems="10"
                                    android:singleLine="true"
                                    android:paddingTop="30dp" />

                                <EditText
                                    android:id="@+id/ET_Address"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_gravity="center_horizontal"
                                    android:hint="Your Address"
                                    android:ems="10"
                                    android:singleLine="true"
                                    android:paddingTop="33dp" />

                                <LinearLayout
                                    android:layout_gravity="center_horizontal"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:background="#FFFFFF"
                                    android:paddingTop="10dp">

                                    <Button
                                        android:id="@+id/button_add"
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:layout_weight="1.0"
                                        android:text="Save"
                                        android:background="#FF8000"
                                        android:textColor="#FFFFFF" />

                                    <Button
                                        android:id="@+id/BT_Clear"
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:layout_weight="1.0"
                                        android:text="Clear"
                                        android:background="#949494"
                                        android:textColor="#FFFFFF" />


                                    <Button
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:layout_weight="1.0"
                                        android:text="View All"
                                        android:id="@+id/button_viewAll"
                                        android:background="#FF8000"
                                        android:textColor="#FFFFFF" />

                                    <Button
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:text="Export"
                                        android:id="@+id/button_export"
                                        android:layout_weight="1.0"
                                        android:background="#949494"
                                        android:textColor="#FFFFFF"/>


                                    <Button
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:text="Delete"
                                        android:id="@+id/button_delete"
                                        android:layout_weight="1.0"
                                        android:background="#FF8000"
                                        android:textColor="#FFFFFF" />

                                </LinearLayout>

                                <TextView
                                    android:id="@+id/ET_IMGPath"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_gravity="center"
                                    android:hint="IMG Path"
                                    android:ems="10"
                                    android:singleLine="true"
                                    android:visibility="invisible" />

                            </LinearLayout>

                            <LinearLayout
                                android:id="@+id/History_Tab"
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:background="#FFFFFF"
                                android:orientation="vertical">

                                <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:text="This is tab 2"
                                    android:layout_gravity="center"/>

                            </LinearLayout>

                            <LinearLayout
                                android:id="@+id/Visits_Tab"
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:background="#FFFFFF"
                                android:orientation="vertical">

                                <TextView
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:text="This is tab 3"
                                    android:layout_gravity="center"/>

                            </LinearLayout>
                        </FrameLayout>
                    </LinearLayout>
                </TabHost>
        </LinearLayout>

這是我在MainActivity中的代碼

TabHost主機=(TabHost)findViewById(R.id.tabHost); host.setup();

    //Personal Data Tab
    TabHost.TabSpec spec = host.newTabSpec("Personal Data");
    spec.setContent(R.id.PersonalData_Tab);
    spec.setIndicator("Personal Data");
    host.addTab(spec);

    //History Tab
    spec = host.newTabSpec("History");
    spec.setContent(R.id.History_Tab);
    spec.setIndicator("History");
    host.addTab(spec);

    //Visits Tab
    spec = host.newTabSpec("Visits");
    spec.setContent(R.id.Visits_Tab);
    spec.setIndicator("Visits");
    host.addTab(spec);

從修改您的代碼

spec.setIndicator(“個人數據”)

spec.setIndicator("Personal Data", ContextCompat.getDrawable(this,R.drawable.yourdrawable));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM