android tablayout 圖片專題及常見問題 - CSDN

2021-01-12 CSDN技術社區

在本教程中,我們將演示如何使用TabLayout渲染4個選項卡-「 Android」,「 Windows」,「 Apple」和「 BlackBerry」,每個選項卡都包含一個文本視圖以顯示一條簡單消息。

PS此項目在Eclipse 3.7中開發,並通過Android 2.3.3進行了測試。

1.標籤圖片

將4個標籤圖像放在「 res / drawable 」文件夾中。 本教程中使用的標籤圖像未遵循Android圖標指南 ,抱歉,我只是不擅長設計:)。

2.用XML標籤圖像

創建4個XM文件以告訴每個選項卡要使用哪個圖像,並將其放入相同的「 res / drawable 」文件夾中。

文件:icon_android_config.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <!-- When selected, you should use bg with grey -->    <item android:drawable="@drawable/ic_tab_android"          android:state_selected="true" />    <!-- When not selected, you should use bg with white -->    <item android:drawable="@drawable/ic_tab_android" /></selector>

文件:icon_apple_config.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <!-- When selected -->    <item android:drawable="@drawable/ic_tab_apple"          android:state_selected="true" />    <!-- When not selected -->    <item android:drawable="@drawable/ic_tab_apple" /></selector>

文件:icon_blackberry_config.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <!-- When selected -->    <item android:drawable="@drawable/ic_tab_blackberry"          android:state_selected="true" />    <!-- When not selected -->    <item android:drawable="@drawable/ic_tab_blackberry" /></selector>

文件:icon_windows_config.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <!-- When selected -->    <item android:drawable="@drawable/ic_tab_windows"          android:state_selected="true" />    <!-- When not selected -->    <item android:drawable="@drawable/ic_tab_windows" /></selector>

3.標籤活動

創建4個活動類,並告訴您單擊選項卡時要使用的活動。 所有4個類都在做相同的事情,顯示一個textview組件。

檔案:AndroidActivity.java

package com.mkyong.android;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class AndroidActivity extends Activity {    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        TextView textview = new TextView(this);        textview.setText("This is Android tab");        setContentView(textview);    }}

文件:AppleActivity.java

package com.mkyong.android;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class AppleActivity extends Activity {    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        TextView textview = new TextView(this);        textview.setText("This is Apple tab");        setContentView(textview);    }}

文件:BlackBerryActivity.java

package com.mkyong.android;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class BlackBerryActivity extends Activity {    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        TextView textview = new TextView(this);        textview.setText("This is BlackBerry tab");        setContentView(textview);    }}

文件:WindowsActivity.java

package com.mkyong.android;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class WindowsActivity extends Activity {    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        TextView textview = new TextView(this);        textview.setText("This is Windows mobile tab");        setContentView(textview);    }}

4.主要活動

創建入口點,上述4片活性的類與連結TabHost , TabSpec等

package com.mkyong.android;import android.app.TabActivity;import android.content.Intent;import android.content.res.Resources;import android.os.Bundle;import android.widget.TabHost;import android.widget.TabHost.TabSpec;public class MainActivity extends TabActivity {	public void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.main);		Resources ressources = getResources(); 		TabHost tabHost = getTabHost(); 				// Android tab		Intent intentAndroid = new Intent().setClass(this, AndroidActivity.class);		TabSpec tabSpecAndroid = tabHost		  .newTabSpec("Android")		  .setIndicator("", ressources.getDrawable(R.drawable.icon_android_config))		  .setContent(intentAndroid);		// Apple tab		Intent intentApple = new Intent().setClass(this, AppleActivity.class);		TabSpec tabSpecApple = tabHost		  .newTabSpec("Apple")		  .setIndicator("", ressources.getDrawable(R.drawable.icon_apple_config))		  .setContent(intentApple);				// Windows tab		Intent intentWindows = new Intent().setClass(this, WindowsActivity.class);		TabSpec tabSpecWindows = tabHost		  .newTabSpec("Windows")		  .setIndicator("", ressources.getDrawable(R.drawable.icon_windows_config))		  .setContent(intentWindows);				// Blackberry tab		Intent intentBerry = new Intent().setClass(this, BlackBerryActivity.class);		TabSpec tabSpecBerry = tabHost		  .newTabSpec("Berry")		  .setIndicator("", ressources.getDrawable(R.drawable.icon_blackberry_config))		  .setContent(intentBerry);			// add all tabs 		tabHost.addTab(tabSpecAndroid);		tabHost.addTab(tabSpecApple);		tabHost.addTab(tabSpecWindows);		tabHost.addTab(tabSpecBerry);				//set Windows tab as default (zero based)		tabHost.setCurrentTab(2);	}}

5. Android布局文件

文件:res / layout / 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" />    </LinearLayout></TabHost>

6. Android清單

最後,將所有內容放入「 AndroidManifest.xml 」中,定義了4個選項卡活動類,並將「 MainActivity 」設置為入口點。

檔案:AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.mkyong.android"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk android:minSdkVersion="10" />    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >                <activity android:name=".AndroidActivity" />        <activity android:name=".WindowsActivity" />        <activity android:name=".AppleActivity" />        <activity android:name=".BlackBerryActivity" />                <activity            android:label="@string/app_name"            android:name=".MainActivity"            android:theme="@android:style/Theme.NoTitleBar" >            <intent-filter >                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

7.演示

默認情況下,Windows選項卡處於選中狀態。

點擊Android標籤。

下載原始碼

下載它– Android-TabLayout-Example.zip (23 KB)

參考文獻 Android TabLayout示例 標籤頁布局教程不完整 Android圖標設計指南 另一個Android標籤布局示例 Android TabHost Javadoc Android TabWidget Javadoc Android FrameLayout Javadoc

翻譯自: https://mkyong.com/android/android-tablayout-example/

相關焦點

  • android 熱啟動黑屏專題及常見問題 - CSDN
    有關於Android第一次啟動出現的黑屏和白屏的過渡問題,看了很多相關的資料和文章,決定將自己的一些見解和處理方式總結一下,以便日後在遇到有個思路,先大致列為幾個部分。
  • android 不同大小的屏幕專題及常見問題 - CSDN
    轉載請註明出處:http://blog.csdn.net/guolin_blog/article/details/8830286原文地址為:http://developer.android.com/training/multiscreen/screensizes.html
  • android 自適應文本框專題及常見問題 - CSDN
    >  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:id="@+id/ll2" android:orientation="vertical"
  • android啟動頁設計專題及常見問題 - CSDN
    轉載請註明出處:http://blog.csdn.net/wangjihuanghun/article/details/63255144啟動頁幾乎成為了每個app的標配,有些商家在啟動頁中增加了開屏廣告以此帶來更多的收入。
  • android 線性布局設置背景圖片專題及常見問題 - CSDN
    orientation屬性 方向,vertical(豎直)或者horizontal(水平)android:gravity 來指定TextView內文字的對齊方式,可選值有 top、 bottom、 left、 right、 center在設置背景圖片的時候,text設置background屬性,ImageView設置src屬性,還有個特殊的。
  • android在根布局添加布局專題及常見問題 - CSDN
    1.item.xml根布局參數沒有添加到父布局1).在item.xml中<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="100dp
  • android grid布局專題及常見問題 - CSDN
    下面也通過一個例子來,創建一個顯示圖片縮略圖的網格。當一個元素被選擇時,顯示該元素在列表中的位置的消息。1)、首先,將上面實踐截取的圖片放入res/drawable/2)、res/layour/main.xml的內容置為如下:這個GridView填滿整個屏幕,而且它的屬性都很好理解,按英文單詞的意思就對了。<?
  • Android - android xml 層級專題及常見問題 - CSDN
    比如說,你需要為不同的屏幕解析度提供替代的圖片資源,為不同的語言提供替代的字符串資源。在運行時,Android 檢測當前設備配置,並為應用程式加載合適的資源。要為特定的配置的確定一系列替代資源,遵循如下的步驟:在res/ 下創建一個新的目錄,以 _ 的方式命名。這裡的 resources_name 是上表中提到的任意資源,如布局、圖片等。
  • android xml 層級專題及常見問題 - CSDN
    比如說,你需要為不同的屏幕解析度提供替代的圖片資源,為不同的語言提供替代的字符串資源。在運行時,Android 檢測當前設備配置,並為應用程式加載合適的資源。要為特定的配置的確定一系列替代資源,遵循如下的步驟:在res/ 下創建一個新的目錄,以 _ 的方式命名。這裡的 resources_name 是上表中提到的任意資源,如布局、圖片等。
  • android app平板改手機專題及常見問題 - CSDN
    轉載請註明出處:http://blog.csdn.net/guolin_blog/article/details/8744943記得我之前參與開發過一個華為的項目,要求程序可以支持好幾種終端設備,其中就包括Android手機和Android Pad。
  • android tv放大專題及常見問題 - CSDN
    這個肯定也許會這麼想我選中了那個item就設置一張焦點框圖片就可以實現,告訴你沒這麼簡單,這個框是根據你選中了那個item而動態畫上去的,而畫的位置是要獲取item的繼承或者實現關係:從圖可以看出來view實現了Drawable.Callback還有KeyEvent.Callback,前者是關於Drawable一些方法比如如何把一張圖片繪製到
  • android 啟動優化專題及常見問題 - CSDN
    -消除啟動時的白屏/黑屏: https://www.jb51.net/article/91568.htm 在用戶點擊手機桌面APP的時候,看到的黑屏或者白屏其實是界面渲染前的第一幀,如果你看懂了文章頭的那2個問題,那麼解決這個問題就非常輕鬆了,無非就是將Theme裡的windowBackground設置成我們想要讓用戶看到的畫面就可以了
  • android 從後臺啟動頁面專題及常見問題 - CSDN
    layout.xml首先創建此頁面的布局文件:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
  • android bootstrap布局專題及常見問題 - CSDN
    因此, 若既想要使用android studio方便燒錄手機的功能, 又想使用 bootstrap 簡單的頁面布局, 那麼問題的關鍵就是要學會在 XML 文件中調用 HTML 格式的文件. 以設計一個按鈕, 並且點擊會跳轉到下一個界面為例說明如何在android studio中調用bootstrap方法, 具體步驟如下.
  • android 監聽屏幕鎖屏專題及常見問題 - CSDN
    > 鎖屏聽音樂(音頻),沒有鎖屏看視頻Android系統亮屏、鎖屏、屏幕解鎖事件(解決部分手機亮屏後未解鎖即進入resume狀態)- http://blog.csdn.net/oracleot/article/details/20378453Android 實現鎖屏的較完美方案- https://segmentfault.com/a/1190000003075989
  • android啟動頁設置專題及常見問題 - CSDN
    首先在 style.xml 文件中創建全屏的主題屬性falsetruetrue然後在 AndroidManifest.xml 中使用android:name=".SplashActivity"android:theme="@style/AppTheme.Full">之後就可以在
  • android 延遲啟動頁專題及常見問題 - CSDN
    在布局文件中將SplashActivity的背景設置為一張圖片(可以是手機壁紙)。xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/splash"tools
  • android 布局 覆蓋專題及常見問題 - CSDN
    ://schemas.android.com/apk/res/android"      android:layout_width="fill_parent"      android:layout_height="fill_parent"  >    <LinearLayout        android:layout_width="fill_parent"        android
  • android絕對布局專題及常見問題 - CSDN
    >02<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"03android:orientation="vertical"04android:layout_width="fill_parent"05android:layout_height
  • android studio布局頁面專題及常見問題 - CSDN
    今天在開發過程中遇到一個大問題,在切換到android studio布局頁面的時候會出現白屏,無法顯示出具體控制項,但是當我切換到其他project的時候一切顯示正常,比較蛋疼,先貼異常代碼: 1、Failed to instantiate one or more