日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

安卓学习-界面-View的自定义

發布時間:2023/12/19 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 安卓学习-界面-View的自定义 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

android的所有UI控件,都是基于View的,因此特意重點學習了下這個,為后面學習其他控件打下基礎。

參照了瘋狂android講義和http://blog.csdn.net/guolin_blog/article/details/17357967,還有幾個例子到后面再寫,主要是現在看不懂

重新時常用覆蓋的方法

package com.example.ddddddd;import android.content.Context; import android.graphics.Canvas; import android.graphics.Rect; import android.util.AttributeSet; import android.util.Log; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; import android.widget.Button;public class MyButton extends Button{public MyButton(Context context, AttributeSet attrs) {super(context, attrs);}//窗口加載這個組件時會執行protected void onFinishInflate() {super.onFinishInflate();Log.d("test", "我被XML加載了");}//看不懂,不知道什么意思,解釋如下//用來檢測View組件和他包含的所有子組件的大小protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);}//當該組件需要分配其子組件位置、大小時會調用該方法protected void onLayout(boolean changed, int left, int top, int right, int bottom) {super.onLayout(changed, left, top, right, bottom);}//該組件大小被改變時會調用該方法protected void onSizeChanged(int w, int h, int oldw, int oldh) {Log.d("test", "被改變大小了,原來w="+oldw+"y="+oldh+" 現在w="+w+"y="+h);super.onSizeChanged(w, h, oldw, oldh);}//要繪制該組件內容時回調該方法protected void onDraw(Canvas canvas) {super.onDraw(canvas);}//光標在這個組件上,并且按鍵被按下時調用該方法public boolean onKeyDown(int keyCode, KeyEvent event) {Log.d("test", "按鍵"+keyCode+"按下了");return super.onKeyDown(keyCode, event);}//光標在這個組件上,并且按鍵松開時調用該方法public boolean onKeyUp(int keyCode, KeyEvent event) {Log.d("test", "按鍵"+keyCode+"松開了");return super.onKeyUp(keyCode, event);}//發生軌跡球事件,不知道是個什么東西public boolean onTrackballEvent(MotionEvent event) {return super.onTrackballEvent(event);}//發生觸摸屏事件,觸發此方法public boolean onTouchEvent(MotionEvent event) {Log.d("test", "我被觸摸了,位置 X:"+event.getX()+" Y:"+event.getY());return super.onTouchEvent(event);}//當得到或失去焦點,觸發protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {if(gainFocus){Log.d("test", "得到焦點");}else{Log.d("test", "失去焦點");}super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);}//該組件放入某個窗口時,觸發protected void onAttachedToWindow() {super.onAttachedToWindow();}//組件從窗口上分離,觸發protected void onDetachedFromWindow() {Log.d("test", "分離");super.onDetachedFromWindow();}//可見性發生改變時觸發,一般調用setVisibility(View.GONE)protected void onVisibilityChanged(View changedView, int visibility) {Log.d("test", "摧毀");super.onVisibilityChanged(changedView, visibility);}} View Code

一個跟隨手指移動的球

?

?

public class MyView extends View{public MyView(Context context) {super(context);}public MyView(Context context, AttributeSet attrs) {super(context, attrs);}//定義一個畫筆private Paint paint=new Paint();private float cx=0;private float cy=0;protected void onDraw(Canvas canvas) {super.onDraw(canvas);//設置畫筆顏色,這里制定紅色 paint.setColor(Color.RED);//繪制一個位置在cx,cy的半徑15,的圓canvas.drawCircle(cx, cy, 15, paint);}public boolean onTouchEvent(MotionEvent event) {cx=event.getX();cy=event.getY();//重繪 invalidate();return true;} } View Code

?標題,參照網上的一個帖子做的

title.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="50dp"android:background="#ffcb05" ><Buttonandroid:id="@+id/button_left"android:layout_width="60dp"android:layout_height="40dp"android:layout_centerVertical="true"android:layout_marginLeft="5dp"android:background="@drawable/back_button"android:textColor="#fff" /><TextViewandroid:id="@+id/title_text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="標題"android:textColor="#fff"android:textSize="20sp" /></RelativeLayout> View Code

組件TitleView.java

public class TitleView extends FrameLayout {//左邊返回的按鈕private Button leftButton;//中間的標題文字private TextView titleText;//初始化public TitleView(Context context, AttributeSet attrs) {super(context, attrs);//獲取XML配置文件LayoutInflater.from(context).inflate(R.layout.title, this);//獲取標題文字組件titleText = (TextView) findViewById(R.id.title_text);//獲取返回按鈕組件leftButton = (Button) findViewById(R.id.button_left);//點擊事件leftButton.setOnClickListener(new OnClickListener() {public void onClick(View v) {((Activity) getContext()).finish();}});}public void setTitleText(String text) {titleText.setText(text);}public void setLeftButtonText(String text) {leftButton.setText(text);}public void setLeftButtonListener(OnClickListener l) {leftButton.setOnClickListener(l);} } View Code

activity_main.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" ><com.example.ddddddd.TitleViewandroid:id="@+id/title_view"android:layout_width="match_parent"android:layout_height="wrap_content" ></com.example.ddddddd.TitleView></RelativeLayout> View Code

?

?

自動以ListView

當快速向右滑動時會顯示一個Del按鈕,點擊可刪除該條信息

1.按鈕的自定義XML頁面

del_button.xml

<Button xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Del" /> View Code

2.ListView要用到的item

my_list_view_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="50dp"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:text="Large Text" android:gravity="center_vertical"android:textAppearance="?android:attr/textAppearanceLarge" /></RelativeLayout> View Code

3.自定義ListView

MyListView.java

這里面用到了OnTouchListener和OnGestureListener

通過onTouch觸發OnGestureListener手勢識別,當onFling執行時判斷是否是橫向快速滑動,是的話創建del_button.xml視圖,并添加按鈕事件

public class MyListView extends ListView implements OnTouchListener,OnGestureListener{private View delButton;private ViewGroup view;private int selectedItem;//刪除事件private OnDeleteListener onDeleteListener; //手勢private GestureDetector gestureDetector; public MyListView(Context context, AttributeSet attrs) {super(context, attrs);gestureDetector = new GestureDetector(getContext(), this); //這句也不要忘記setOnTouchListener(this); }public void setOnDeleteListener(OnDeleteListener onDeleteListener) {this.onDeleteListener = onDeleteListener;}//必須寫這個方法,然后將觸摸的事件交給手勢來做public boolean onTouch(View v, MotionEvent event) {//交給手勢來處理return gestureDetector.onTouchEvent(event);}/*****************OnGestureListener接口的實現類******************///每次按下都會觸發public boolean onDown(MotionEvent e) {Log.v("wjj", "onDown");//按下就先清除原來的刪除按鈕if(delButton != null){view.removeView(delButton);delButton=null;}//pointToPosition 根據x,y獲取item的位置selectedItem = pointToPosition((int) e.getX(), (int) e.getY()); Log.v("wjj", "selectedItem="+selectedItem);return false;}//類似單擊,按下后過一會才彈起,但還沒達到longpress的標準//介于onSingleTapUp和onLongPress之間吧public void onShowPress(MotionEvent e) {Log.v("wjj", "onShowPress");}//類似單擊,按下后馬上彈起public boolean onSingleTapUp(MotionEvent e) {Log.v("wjj", "onSingleTapUp");return false;}//手指比較慢的滑動,可以縱向橫向public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {//Log.v("wjj", "onScroll");return false;}//長按記錄public void onLongPress(MotionEvent e) {Log.v("wjj", "onLongPress"); //長按時顯示刪除按鈕 }//手指快速的滑動,可以縱向橫向//4個參數相當于向量 //e1為向量的起點,e2為向量的終點,velocityX為向量水平方向的速度,velocityY為向量垂直方向的速度public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {Log.v("wjj", "onFling");//水平方向速度大于垂直方向速度,認為是橫向滑動if(Math.abs(velocityX)>Math.abs(velocityY)){//獲取刪除按鈕視圖delButton=LayoutInflater.from(getContext()).inflate(R.layout.del_button,null);delButton.setOnClickListener(new OnClickListener() {public void onClick(View v) {Log.v("刪除", selectedItem+"");view.removeView(delButton);delButton=null;onDeleteListener.onDelete(selectedItem);}});//設置位置RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); params.addRule(RelativeLayout.CENTER_VERTICAL); //獲得滑動位置的item//selectedItem onDown時獲取,//當前顯示的第一個getFirstVisiblePosition//getChildAt,比如當前總共顯示10個,那么不管在那個位置,都是從0到9view=(ViewGroup)getChildAt(selectedItem-getFirstVisiblePosition());TextView tv=(TextView)view.findViewById(R.id.textView1);Log.v("wjj", "TextView "+tv.getText());view.addView(delButton,params);}return false;}//增加刪除事件,方便外部調用public interface OnDeleteListener { void onDelete(int index); } } View Code

4.自定義Adapter

MyAdapter.java

public class MyAdapter extends ArrayAdapter<String> {public MyAdapter(Context context, int resource, List<String> objects) {super(context, resource, objects);// TODO 自動生成的構造函數存根 }@Overridepublic View getView(int position, View convertView, ViewGroup parent) {View view;//是否該視圖已經存在if(convertView==null){//不存在,直接從xml加載視圖view=LayoutInflater.from(getContext()).inflate(R.layout.my_list_view_item, null);}else{view=convertView;}//獲取textTextView tv=(TextView)view.findViewById(R.id.textView1);//設置text的值 tv.setText(getItem(position));//返回自定義viewreturn view;}} View Code

5.主界面

activity_main.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" ><com.example.ddddddd.MyListViewandroid:id="@+id/listView1"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_alignParentLeft="true" ></com.example.ddddddd.MyListView></RelativeLayout> View Code

MainActivity.java

public class MainActivity extends Activity {MyListView lv;private List<String> contentList = new ArrayList<String>(); protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initList();lv=(MyListView)findViewById(R.id.listView1);lv.setAdapter(new MyAdapter(this, 0, contentList));lv.setOnDeleteListener(new OnDeleteListener() {public void onDelete(int index) {//刪除 contentList.remove(index);((MyAdapter)lv.getAdapter()).notifyDataSetChanged();}});}private void initList() { for(int i=1;i<=100;i++){contentList.add("Content Item "+i); }} } View Code

?

轉載于:https://www.cnblogs.com/weijj/p/3921983.html

總結

以上是生活随笔為你收集整理的安卓学习-界面-View的自定义的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。