TabHost標(biāo)簽

一 . 自定義標(biāo)簽
二 . ViewPager
三. ScrollView

image.png

----------------效果圖----如下---------------

image.png

我們定義一個(gè)selector選擇器
-----------bg.xml-------------

<item android:state_selected="true"
android:drawable="@drawable/bg_selected"
></item>
<item android:drawable="@drawable/bg_normal"></item>
在 activity.xml-----------------------代碼如下

···
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<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="0dp"
        android:layout_weight="1" >
        <!-- 首頁(yè) -->
        <LinearLayout 
            android:id="@+id/line1"
            android:layout_width="match_parent"
              android:layout_height="match_parent"
           
            >
            <TextView 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="首頁(yè)"
                android:textSize="30sp"
                android:gravity="center"
                />
            
        </LinearLayout>
        <!-- 中間-->
        <LinearLayout 
            android:id="@+id/line2"
            android:layout_width="match_parent"
              android:layout_height="match_parent"
           
            >
            <TextView 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="中間"
                android:textSize="30sp"
                android:gravity="center"
                />
            
        </LinearLayout>
        <!-- 最后 -->
        <LinearLayout 
            android:id="@+id/line3"
            android:layout_width="match_parent"
              android:layout_height="match_parent"
           
            >
            <TextView 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="最后"
                android:textSize="30sp"
                android:gravity="center"
                />
            
        </LinearLayout>
    </FrameLayout>
</LinearLayout>

</TabHost>
···
在定義一個(gè)tab.xml---------------------
*********代碼如下****************
···
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bg"
>

  <TextView 
      android:id="@+id/tv_title"
      android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:gravity="center"
     android:text="首頁(yè)"
     android:textColor="@android:color/white"
     android:textSize="22sp"
      />

</LinearLayout>

···
MainActivity***代碼如下************
···
package com.example.app08_tabhost;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public class MainActivity extends Activity {
private TabHost tabhost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabhost=(TabHost) findViewById(R.id.tabhost);
tabhost.setup();//找到 Tabwidget和framelayout
//添加標(biāo)簽頁(yè)
TabSpec tab1 = tabhost.newTabSpec("tab1");
//指定標(biāo)簽
// tab1.setIndicator("首頁(yè)",getResources().getDrawable(R.drawable.i1));
tab1.setIndicator(createView("首頁(yè)"));

    //指定標(biāo)簽頁(yè)內(nèi)容
    tab1.setContent(R.id.line1);
    tabhost.addTab(tab1);
    //添加標(biāo)簽頁(yè)
    TabSpec tab2 = tabhost.newTabSpec("tab2");
    //指定標(biāo)簽
      tab2.setIndicator(createView("第二頁(yè)"));

// tab2.setIndicator("中間",getResources().getDrawable(R.drawable.i2));
//指定標(biāo)簽頁(yè)內(nèi)容
tab2.setContent(R.id.line2);
tabhost.addTab(tab2);
//添加標(biāo)簽頁(yè)
TabSpec tab3 = tabhost.newTabSpec("tab3");
//指定標(biāo)簽
tab3.setIndicator(createView("第三頁(yè)"));
// tab3.setIndicator("最后頁(yè)",getResources().getDrawable(R.drawable.i7));
//指定標(biāo)簽頁(yè)內(nèi)容
tab3.setContent(R.id.line3);
tabhost.addTab(tab3);

}
private View createView(String text){
    View view =View.inflate(this, R.layout.tab, null);
    TextView tv_title=(TextView) view.findViewById(R.id.tv_title);
    tv_title.setText(text);
            return view;
    
}

}

···

************效果圖*************


image.png

我們定義一個(gè)選擇器 selector和 shape
*********代碼如下-----------
************point_selector.xml************
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="false"
android:drawable="@drawable/point_shape"></item>
<item android:state_enabled="true"
android:drawable="@drawable/point_shape2"></item>

</selector>
************point_shape.xml************
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"

>
<size 
    android:width="8dp"
    android:height="8dp"
    />
<solid 
    android:color="#44000000"
    />

</shape>
************<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"

>
<size 
    android:width="8dp"
    android:height="8dp"
    />
<solid 
    android:color="#44000000"
    />

</shape>
**************point_shape2.xml*************
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"

>
<size 
    android:width="8dp"
    android:height="8dp"
    />
<solid 
    android:color="#ff0000"
    />

</shape>
在******activity.xml***********
···
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >

<android.support.v4.view.ViewPager
    android:id="@+id/vp_main"
    android:layout_width="match_parent"
    android:layout_height="180dp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@id/vp_main"
    android:background="#44000000"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:padding="3dp"
        android:text="廣告標(biāo)題"
        android:textColor="#ffffff" />
    <LinearLayout 
        android:id="@+id/ll_point_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center_horizontal"
        
        >
        
    </LinearLayout>
</LinearLayout>

</RelativeLayout>
···
在MainActivity中***********代碼如下********
···
package com.example.test13_viewpager;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
private ViewPager vp_main;
private TextView tv_title;
private LinearLayout ll_point_group;
private ArrayList<ImageView> imageViews;
private int prePosition=0;//上一次高亮顯示的位置
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg){
int item = vp_main.getCurrentItem() + 1;
vp_main.setCurrentItem(item);
//延遲發(fā)消息
handler.sendEmptyMessageDelayed(0, 3000);
};
};
//是否已經(jīng)拖拽
private boolean isDragging =false;
//圖片資源id
private int[] imgeId={R.drawable.a,
R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e};
//圖片標(biāo)題集合
private String[] imgDescrintions={
"周末大放送","家電買(mǎi)一送一","京東火鍋節(jié)","預(yù)約新機(jī)","有范"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//ListView的使用
//1.在布局文件中定義ViewPager
//2.在代碼中實(shí)例化viewpager
setContentView(R.layout.activity_main);
vp_main=(ViewPager) findViewById(R.id.vp_main);
tv_title=(TextView) findViewById(R.id.tv_title);
ll_point_group=(LinearLayout) findViewById(R.id.ll_point_group);
//3.準(zhǔn)備數(shù)據(jù)
imageViews=new ArrayList<ImageView>();
for (int i = 0; i < imgeId.length; i++) {
ImageView imageView = new ImageView(this);
//設(shè)置背景圖片
imageView.setBackgroundResource(imgeId[i]);
//添加到集合
imageViews.add(imageView);
//添加小圓點(diǎn)
ImageView point=new ImageView(this);
//設(shè)置背景資源
point.setBackgroundResource(R.drawable.point_selector);
//間距
LinearLayout.LayoutParams params=new
LinearLayout.LayoutParams(8,8);
if (i==0) {
point.setEnabled(true);//紅色
}else{
point.setEnabled(false);//灰色
params.leftMargin=8;//左外邊距
}

        point.setLayoutParams(params);
        ll_point_group.addView(point);
    }
    //4.設(shè)置適配器(pagerAdapter) Item布局 綁定數(shù)據(jù)
    vp_main.setAdapter(new MyAdapter());
    
    //設(shè)置監(jiān)聽(tīng)viewPager頁(yè)面的改變
    vp_main.setOnPageChangeListener(new MyPageChangeListener());
    //設(shè)置中間位置   保證是imageViews的整數(shù)倍
    int item=Integer.MAX_VALUE / 2
            - Integer.MAX_VALUE /2 % imageViews.size();
    vp_main.setCurrentItem(item);//不斷的加一 加載
    tv_title.setText(imgDescrintions[prePosition]);
   //發(fā)送消息
    handler.sendEmptyMessageDelayed(0, 3000);

}


class MyPageChangeListener implements OnPageChangeListener{
          /**
                    當(dāng)頁(yè)面滾動(dòng)狀態(tài)變化的時(shí)候回調(diào)這個(gè)方法
          靜止-》滑動(dòng)   或 滑動(dòng) -> 靜止  或  靜止--》拖拽
          * 
          */
    @Override
    public void onPageScrollStateChanged(int state) {
        if (state==ViewPager.SCROLL_STATE_DRAGGING) {//拖拽
            isDragging=true;
        } else if (state==ViewPager.SCROLL_STATE_SETTLING){//滾動(dòng)
             
        }else if (state==ViewPager.SCROLL_STATE_IDLE&&isDragging ){
            //靜止并拖拽
            isDragging=false;
            //重新發(fā)送事先移除之前的消息
            handler.removeCallbacksAndMessages(null);
            handler.sendEmptyMessageDelayed(0, 3000);
            
        }
    }
                  /**
                   * 當(dāng)前頁(yè)面滾動(dòng)了的時(shí)候回調(diào)的方法
                   * position  當(dāng)前頁(yè)面的位置
                   * positionOffset   滑動(dòng)頁(yè)面的半分比
                   * positionOffsetPixels 在屏幕上滑動(dòng)的像素
                   */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        
    }
    /**
       當(dāng)某個(gè)頁(yè)面被選中了的時(shí)候
     postion   被選中頁(yè)面的位置
    */

    @Override// 當(dāng)某個(gè)頁(yè)面被選中了的時(shí)候
    public void onPageSelected(int position) {
        int realPosition =position % imageViews.size();
        //設(shè)置對(duì)頁(yè)面的文本信息
        tv_title.setText(imgDescrintions[realPosition]);
        //把上一個(gè)高亮的設(shè)置灰色
        ll_point_group.getChildAt(realPosition)
        .setEnabled(false);
        //當(dāng)前的設(shè)置為紅色
    ll_point_group.getChildAt(realPosition)
    .setEnabled(true);
    prePosition=realPosition;
        
    }
    
}
class MyAdapter extends PagerAdapter{

    @Override//得到圖片的總數(shù)
    public int getCount() {
        
        return Integer.MAX_VALUE;//int類(lèi)型的最大值
    
    }

    @Override//比較 View 和Object 是否是同一個(gè)實(shí)例
    //view 頁(yè)面        object 是instantiateItem返回的結(jié)果
    public boolean isViewFromObject(View view, Object object) {
        
        return view == object;
    }
        /**類(lèi)似getView的方法
         * container  viewPager自身
         * position 當(dāng)前實(shí)例化頁(yè)面的位置(下標(biāo))
         *
         * */
    @Override//實(shí)例化
    public Object instantiateItem(ViewGroup container, int position) {
        int realPosition = position  % imageViews.size();
        
        ImageView imageView = imageViews.get(realPosition);
         
         container.addView(imageView);//添加到ViewPager
         //觸摸監(jiān)聽(tīng)
         imageView.setOnTouchListener(new View.OnTouchListener() {
            
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {//獲取動(dòng)作
                case MotionEvent.ACTION_DOWN://按下
                    //移除消息
                    handler.removeCallbacksAndMessages(null);
                    break;
                case MotionEvent.ACTION_MOVE://移動(dòng)
                    //移除消息
                //  handler.removeCallbacksAndMessages(null);
                    break;
                case MotionEvent.ACTION_CANCEL://取消
                    //移除消息
                    handler.removeCallbacksAndMessages(null);
                    handler.sendEmptyMessageDelayed(0, 3000);
                        break;
                case MotionEvent.ACTION_UP://抬起
                    handler.removeCallbacksAndMessages(null);
                    handler.sendEmptyMessageDelayed(0, 3000);
                    break;

                }
                
                return false;
            }
        });
         imageView.setTag(position);//設(shè)置下標(biāo)
         //給圖片設(shè)置監(jiān)聽(tīng)
         imageView.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                
             int position =(Integer) v.getTag() % imageViews.size();    
                //獲取文字
            String text= imgDescrintions[position];
            Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
            
            }
        });
        // Log.e("TAG","instantiateItem---->"+position+"imageView-->"+imageView);
        return imageView;
    }
    
    
    @Override//釋放資源              1.container值得是Viewpager自身  2.下標(biāo)译仗,要釋放的位置    3.object 要釋放的頁(yè)面
    public void destroyItem(ViewGroup container, int position, Object object) {
        
        container.removeView((View) object);
        
    }
}

}

···
***************ScrollView**************
效果圖*******************如下


image.png

scroll.xml***********代碼如下********
···
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

      <Button 
       android:layout_width="match_parent"
      android:layout_height="wrap_content"
        android:text="按鈕"
       />
         <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="里面文字自己定義" />

</LinearLayout>
</ScrollView>
</RelativeLayout>

···
在 MainActivity中**************
findViewById ().setOnClickListener(this);
public void onClick(View v){
startActivity(new Intent (MainActivity.this,SecondActivity.class))
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末桶蝎,一起剝皮案震驚了整個(gè)濱河市铣减,隨后出現(xiàn)的幾起案子扣猫,更是在濱河造成了極大的恐慌手幢,老刑警劉巖咆霜,帶你破解...
    沈念sama閱讀 218,204評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件涎嚼,死亡現(xiàn)場(chǎng)離奇詭異杉辙,居然都是意外死亡席噩,警方通過(guò)查閱死者的電腦和手機(jī)班缰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)悼枢,“玉大人鲁捏,你說(shuō)我怎么就攤上這事∠糗剑” “怎么了给梅?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,548評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)双揪。 經(jīng)常有香客問(wèn)我动羽,道長(zhǎng),這世上最難降的妖魔是什么渔期? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,657評(píng)論 1 293
  • 正文 為了忘掉前任运吓,我火速辦了婚禮,結(jié)果婚禮上疯趟,老公的妹妹穿的比我還像新娘拘哨。我一直安慰自己,他們只是感情好信峻,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,689評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布倦青。 她就那樣靜靜地躺著,像睡著了一般盹舞。 火紅的嫁衣襯著肌膚如雪产镐。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,554評(píng)論 1 305
  • 那天踢步,我揣著相機(jī)與錄音癣亚,去河邊找鬼。 笑死获印,一個(gè)胖子當(dāng)著我的面吹牛述雾,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 40,302評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼玻孟,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼唆缴!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起取募,我...
    開(kāi)封第一講書(shū)人閱讀 39,216評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎蟆技,沒(méi)想到半個(gè)月后玩敏,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,661評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡质礼,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,851評(píng)論 3 336
  • 正文 我和宋清朗相戀三年旺聚,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片眶蕉。...
    茶點(diǎn)故事閱讀 39,977評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡砰粹,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出造挽,到底是詐尸還是另有隱情碱璃,我是刑警寧澤,帶...
    沈念sama閱讀 35,697評(píng)論 5 347
  • 正文 年R本政府宣布饭入,位于F島的核電站嵌器,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏谐丢。R本人自食惡果不足惜爽航,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,306評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望乾忱。 院中可真熱鬧讥珍,春花似錦、人聲如沸窄瘟。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,898評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)蹄葱。三九已至纲酗,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間新蟆,已是汗流浹背觅赊。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,019評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留琼稻,地道東北人吮螺。 一個(gè)月前我還...
    沈念sama閱讀 48,138評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親鸠补。 傳聞我的和親對(duì)象是個(gè)殘疾皇子萝风,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,927評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • //通過(guò)獲得資源文件進(jìn)行設(shè)置。根據(jù)不同的情況R.color.red也可以是R.string.red或者R.draw...
    gogoingmonkey閱讀 1,942評(píng)論 0 2
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,133評(píng)論 25 707
  • afinalAfinal是一個(gè)android的ioc紫岩,orm框架 https://github.com/yangf...
    passiontim閱讀 15,429評(píng)論 2 45
  • 宋凡平走到了城東的長(zhǎng)途汽車(chē)站规惰,他看到一個(gè)戴紅袖章的人手里拿著木棍站在臺(tái)階上,這個(gè)人看到宋凡平從橋上走下來(lái)時(shí)泉蝌,立刻轉(zhuǎn)...
    陶子青青閱讀 1,152評(píng)論 0 0
  • “HTML的標(biāo)簽是給內(nèi)容添加相關(guān)語(yǔ)義的而不是改變內(nèi)容樣式的歇万,需要給內(nèi)容添加樣式需要用到CSS”以江哥的教誨來(lái)引出C...
    心之壁閱讀 104評(píng)論 0 0