1、定義
<meta> 標(biāo)簽提供關(guān)于 HTML 文檔的元數(shù)據(jù)。它不會顯示在頁面上,但是對于機器是可讀的。可用于瀏覽器(如何顯示內(nèi)容或重新加載頁面),搜索引擎(關(guān)鍵詞),或其他 web 服務(wù)。
2、作用
meta里的數(shù)據(jù)是供機器解讀的,告訴機器該如何解析這個頁面,還有一個用途是可以添加服務(wù)器發(fā)送到瀏覽器的http頭部內(nèi)容,例如我們?yōu)轫撁嬷刑砑尤缦耺eta標(biāo)簽:
瀏覽器的頭部就會包括這些:
只有瀏覽器可以接受這些附加的頭部字段,并能以適當(dāng)?shù)姆绞绞褂盟鼈儠r,這些字段才有意義。
3、meta的必需屬性和可選屬性
meta的必需屬性是content,當(dāng)然并不是說meta標(biāo)簽里一定要有content,而是當(dāng)有http-equiv或name屬性的時候,一定要有content屬性對其進行說明。例如:
必需屬性
<meta name="keywords" content="HTML,ASP,PHP,SQL">
這里面content里的屬性就是對keywords進行的說明,所以呢也可以理解成一個鍵值對吧,就是{keywords:"HTML,ASP,PHP,SQL"}。
可選屬性
在W3school中,對于meta的可選屬性說到了三個,分別是http-equiv、name和scheme。考慮到scheme不是很常用,所以就只說下前兩個屬性吧。
http-equiv
http-equiv屬性是添加http頭部內(nèi)容,對一些自定義的,或者需要額外添加的http頭部內(nèi)容,需要發(fā)送到瀏覽器中,我們就可以是使用這個屬性。在上面的meta作用中也有簡單的說明,那么現(xiàn)在再舉個例子。例如我們不想使用js來重定向,用http頭部內(nèi)容控制,那么就可以這樣控制:
<meta http-equiv="Refresh" content="5;url=http://blog.yangchen123h.cn" />
在頁面中加入這個后,5秒鐘后就會跳轉(zhuǎn)到指定頁面啦,效果可看W3school的例子
name
第二個可選屬性是name,這個屬性是供瀏覽器進行解析,對于一些瀏覽器兼容性問題,name屬性是最常用的,當(dāng)然有個前提就是瀏覽器能夠解析你寫進去的name屬性才可以,不然就是沒有意義的。還是舉個例子吧:
<meta name="renderer" content="webkit">
這個meta標(biāo)簽的意思就是告訴瀏覽器,用webkit內(nèi)核進行解析,當(dāng)然前提是瀏覽器有webkit內(nèi)核才可以,不然就是沒有意義的啦。當(dāng)然看到這個你可能會有疑問,這個renderer是從哪里冒出來的,我要怎么知道呢?這個就是在對應(yīng)的瀏覽器的開發(fā)文檔里就會有表明的,例如這個renderer是在360瀏覽器里說明的。360瀏覽器內(nèi)核控制Meta標(biāo)簽說明文檔
常用meta標(biāo)簽大總結(jié)
接下來就是常用的meta標(biāo)簽大總結(jié)啦,我會盡可能的做到全
charset
charset是聲明文檔使用的字符編碼,解決亂碼問題主要用的就是它,值得一提的是,這個charset一定要寫第一行,不然就可能會產(chǎn)生亂碼了。
charset有兩種寫法
兩個都是等效的。
百度禁止轉(zhuǎn)碼
百度會自動對網(wǎng)頁進行轉(zhuǎn)碼,這個標(biāo)簽是禁止百度的自動轉(zhuǎn)碼
<meta http-equiv="Cache-Control" content="no-siteapp" />
SEO 優(yōu)化部分
viewport
viewport主要是影響移動端頁面布局的,例如:
content 參數(shù):
各瀏覽器平臺
Microsoft Internet Explorer
Google Chrome
360瀏覽器
UC手機瀏覽器
UCBrowser_U3_API
QQ手機瀏覽器
Apple iOS
Google Android
App Links
最后——移動端常用的meta
第一種方法:在XML文件下添加:
android:focusable="true" android:focusableInTouchMode="true"
第二種方法:直接關(guān)閉輸入法
在onCreate中加上:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
第三中方法:在Edittext中設(shè)置
.setInputType(InputType.TYPE_NULL);
但是會實現(xiàn)效果,但是會導(dǎo)致光標(biāo)也無法顯示,如果想顯示光標(biāo),你就需要定義一個方法,在方法內(nèi)setInputType(禁止)
public void disableShowInput() { if (android.os.Build.VERSION.SDK_INT <=10) { editText.setInputType(InputType.TYPE_NULL); } else { Class<EditText> cls=EditText.class; Method method; try { method=cls.getMethod("setShowSoftInputOnFocus", boolean.class); method.setAccessible(true); method.invoke(editText, false) } catch (Exception e) {//TODO: handle exception } try { method=cls.getMethod("setSoftInputShownOnFocus", boolean.class); method.setAccessible(true); method.invoke(editText, false); } catch (Exception e) {//TODO: handle exception } } }
這時 你會發(fā)現(xiàn),光標(biāo)是出來了 但是輸入文字,光標(biāo)永遠(yuǎn)在前面,怎么辦,解決思路,監(jiān)聽文字文本內(nèi)容變化監(jiān)聽方法,在afterTextChanged文字輸入完后,把光標(biāo)移到最后.setSelection(Edittext,Edittext.length());看代碼:
Edittext.addTextChangedListener(watcher); private TextWatcher watcher=new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { Edittext.setSelection(Edittext, Edittext.length()); } };
附加:重寫Edittext 在輸入框內(nèi)增加刪除文字按鈕,就是在http://lib.csdn.net/base/android系統(tǒng)的輸入框右邊加入一個小圖標(biāo),點擊小圖標(biāo)可以清除輸入框里面的內(nèi)容具體看代碼:
import android.content.Context; import android.graphics.drawable.Drawable; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.View.OnFocusChangeListener; import android.view.animation.Animation; import android.view.animation.CycleInterpolator; import android.view.animation.TranslateAnimation; import android.widget.EditText; public class RMEditText extends EditText implements OnFocusChangeListener, TextWatcher { /** * 刪除按鈕的引用 */ private Drawable mClearDrawable; /** * 控件是否有焦點 */ private boolean falg; public ClearEditText(Context context) { this(context, null); } public ClearEditText(Context context, AttributeSet attrs) { //這里構(gòu)造方法也很重要,不加這個很多屬性不能再XML里面定義 this(context, attrs, android.R.attr.editTextStyle); } public ClearEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { //獲取EditText的DrawableRight,假如沒有設(shè)置我們就使用默認(rèn)的圖片 mClearDrawable=getCompoundDrawables()[2]; if (mClearDrawable==null) { // throw new NullPointerException("You can add drawableRight attribute in XML"); mClearDrawable=getResources().getDrawable(R.drawable.delete_selector); } mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight()); //默認(rèn)設(shè)置隱藏圖標(biāo) setClearIconVisible(false); //設(shè)置焦點改變的監(jiān)聽 setOnFocusChangeListener(this); //設(shè)置輸入框里面內(nèi)容發(fā)生改變的監(jiān)聽 addTextChangedListener(this); } /** * 因為我們不能直接給EditText設(shè)置點擊事件,所以我們用記住我們按下的位置來模擬點擊事件 * 當(dāng)我們按下的位置 在 EditText的寬度 - 圖標(biāo)到控件右邊的間距 - 圖標(biāo)的寬度 和 * EditText的寬度 - 圖標(biāo)到控件右邊的間距之間我們就算點擊了圖標(biāo),豎直方向就沒有考慮 */ @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction()==MotionEvent.ACTION_UP) { if (getCompoundDrawables()[2] !=null) { boolean touchable=event.getX() > (getWidth() - getTotalPaddingRight()) && (event.getX() < ((getWidth() - getPaddingRight()))); if (touchable) { this.setText(""); } } } return super.onTouchEvent(event); } /** * 當(dāng)ClearEditText焦點發(fā)生變化的時候,判斷里面字符串長度設(shè)置清除圖標(biāo)的顯示與隱藏 */ @Override public void onFocusChange(View v, boolean hasFocus) { this.falg=hasFocus; if (hasFocus) { setClearIconVisible(getText().length() > 0); } else { setClearIconVisible(false); } } /** * 設(shè)置清除圖標(biāo)的顯示與隱藏,調(diào)用setCompoundDrawables為EditText繪制上去 * @param visible */ protected void setClearIconVisible(boolean visible) { Drawable right=visible ? mClearDrawable : null; setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], right, getCompoundDrawables()[3]); } /** * 當(dāng)輸入框里面內(nèi)容發(fā)生變化的時候回調(diào)的方法 */ @Override public void onTextChanged(CharSequence s, int start, int count, int after) { if(falg){ setClearIconVisible(s.length() > 0); } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } }
在xml文件中這樣用
<com.example.edittext.RMEditText android:hint="輸入文字" android:layout_width="fill_parent" android:layout_height="wrap_content" > </>