編輯:關於Android編程
<framelayout android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"> </framelayout>以上是布局代碼,下面就介紹如何通過點擊標簽切換Fragment:我們會發現,初始的時候是選中第一個標簽頁,圖片和字體的顏色區別於另外兩個標簽頁,所以我們要做的就是切換標簽的時候,就改變標簽的狀態主要改兩個內容:圖片文字顏色
/** * 添加或者顯示碎片 * * @param transaction * @param fragment */ private void addOrShowFragment(FragmentTransaction transaction, Fragment fragment) { if (currentFragment == fragment) return; if (!fragment.isAdded()) { // 如果當前fragment未被添加,則添加到Fragment管理器中 transaction.hide(currentFragment) .add(R.id.content_layout, fragment).commit(); } else { transaction.hide(currentFragment).show(fragment).commit(); } currentFragment = fragment; }
package com.xiaowu.bottomtab.demo; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTransaction; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; /** * 主Activity * * @author wwj_748 * */ public class MainActivity extends FragmentActivity implements OnClickListener { // 三個tab布局 private RelativeLayout knowLayout, iWantKnowLayout, meLayout; // 底部標簽切換的Fragment private Fragment knowFragment, iWantKnowFragment, meFragment, currentFragment; // 底部標簽圖片 private ImageView knowImg, iWantKnowImg, meImg; // 底部標簽的文本 private TextView knowTv, iWantKnowTv, meTv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initUI(); initTab(); } /** * 初始化UI */ private void initUI() { knowLayout = (RelativeLayout) findViewById(R.id.rl_know); iWantKnowLayout = (RelativeLayout) findViewById(R.id.rl_want_know); meLayout = (RelativeLayout) findViewById(R.id.rl_me); knowLayout.setOnClickListener(this); iWantKnowLayout.setOnClickListener(this); meLayout.setOnClickListener(this); knowImg = (ImageView) findViewById(R.id.iv_know); iWantKnowImg = (ImageView) findViewById(R.id.iv_i_want_know); meImg = (ImageView) findViewById(R.id.iv_me); knowTv = (TextView) findViewById(R.id.tv_know); iWantKnowTv = (TextView) findViewById(R.id.tv_i_want_know); meTv = (TextView) findViewById(R.id.tv_me); } /** * 初始化底部標簽 */ private void initTab() { if (knowFragment == null) { knowFragment = new ZhidaoFragment(); } if (!knowFragment.isAdded()) { // 提交事務 getSupportFragmentManager().beginTransaction() .add(R.id.content_layout, knowFragment).commit(); // 記錄當前Fragment currentFragment = knowFragment; // 設置圖片文本的變化 knowImg.setImageResource(R.drawable.btn_know_pre); knowTv.setTextColor(getResources() .getColor(R.color.bottomtab_press)); iWantKnowImg.setImageResource(R.drawable.btn_wantknow_nor); iWantKnowTv.setTextColor(getResources().getColor( R.color.bottomtab_normal)); meImg.setImageResource(R.drawable.btn_my_nor); meTv.setTextColor(getResources().getColor(R.color.bottomtab_normal)); } } @Override public void onClick(View view) { switch (view.getId()) { case R.id.rl_know: // 知道 clickTab1Layout(); break; case R.id.rl_want_know: // 我想知道 clickTab2Layout(); break; case R.id.rl_me: // 我的 clickTab3Layout(); break; default: break; } } /** * 點擊第一個tab */ private void clickTab1Layout() { if (knowFragment == null) { knowFragment = new ZhidaoFragment(); } addOrShowFragment(getSupportFragmentManager().beginTransaction(), knowFragment); // 設置底部tab變化 knowImg.setImageResource(R.drawable.btn_know_pre); knowTv.setTextColor(getResources().getColor(R.color.bottomtab_press)); iWantKnowImg.setImageResource(R.drawable.btn_wantknow_nor); iWantKnowTv.setTextColor(getResources().getColor( R.color.bottomtab_normal)); meImg.setImageResource(R.drawable.btn_my_nor); meTv.setTextColor(getResources().getColor(R.color.bottomtab_normal)); } /** * 點擊第二個tab */ private void clickTab2Layout() { if (iWantKnowFragment == null) { iWantKnowFragment = new IWantKnowFragment(); } addOrShowFragment(getSupportFragmentManager().beginTransaction(), iWantKnowFragment); knowImg.setImageResource(R.drawable.btn_know_nor); knowTv.setTextColor(getResources().getColor(R.color.bottomtab_normal)); iWantKnowImg.setImageResource(R.drawable.btn_wantknow_pre); iWantKnowTv.setTextColor(getResources().getColor( R.color.bottomtab_press)); meImg.setImageResource(R.drawable.btn_my_nor); meTv.setTextColor(getResources().getColor(R.color.bottomtab_normal)); } /** * 點擊第三個tab */ private void clickTab3Layout() { if (meFragment == null) { meFragment = new MeFragment(); } addOrShowFragment(getSupportFragmentManager().beginTransaction(), meFragment); knowImg.setImageResource(R.drawable.btn_know_nor); knowTv.setTextColor(getResources().getColor(R.color.bottomtab_normal)); iWantKnowImg.setImageResource(R.drawable.btn_wantknow_nor); iWantKnowTv.setTextColor(getResources().getColor( R.color.bottomtab_normal)); meImg.setImageResource(R.drawable.btn_my_pre); meTv.setTextColor(getResources().getColor(R.color.bottomtab_press)); } /** * 添加或者顯示碎片 * * @param transaction * @param fragment */ private void addOrShowFragment(FragmentTransaction transaction, Fragment fragment) { if (currentFragment == fragment) return; if (!fragment.isAdded()) { // 如果當前fragment未被添加,則添加到Fragment管理器中 transaction.hide(currentFragment) .add(R.id.content_layout, fragment).commit(); } else { transaction.hide(currentFragment).show(fragment).commit(); } currentFragment = fragment; } }
本文實例講述了Android編程之OpenGL繪圖技巧。分享給大家供大家參考,具體如下:很久不用OpenGL ES繪圖,怕自己忘記了,於是重新復習一遍,順便原理性的東西總
一、背景最近做項目需要用到選擇圖片上傳,類似於微信、微博那樣的圖片選擇器,ContentResolver讀取本地圖片資源並用RecyclerView+Glide加載圖片顯
如今的人們幾乎每天都會用手機拍攝無數張照片,但是照片一多管理就是一件麻煩事。一般來說,用戶管理照片不是系統的圖庫就是快圖應用,後者雖然浏覽速度快但管理功能卻
概述把圖片切分很多份,點擊交換拼成一張完整的;這樣關卡也很容易設計, 3*3 ; 4*4 ; 5*5 ; 6*6 ;一直下去效果加了個切換動畫,效果還是不錯的,其實游戲就