Android一些常見的小技巧(一)

做項目過程中,遇到一些特定場景的需求。下面分幾期總結下來,與大家一起分享。

1.listview(豎直方向) 中每個item佈局要間距一段距離。

       <yx.communitysocket.com.socket.view.MyListView
             android:id="@+id/hot_comments_lv"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_marginTop="@dimen/padding5"
             android:divider="#00000000"
             android:dividerHeight="@dimen/padding5"/>

2.滾動條設置沒有默認的粗滾動條(豎線)

	    mScrollView= (ScrollView)findViewById(R.id.comments_scrollView);
	    mScrollView.setVerticalScrollBarEnabled(false);

3.LinearLayout整體響應點擊事件

	<RelativeLayout
            android:clickable="true"
            android:onClick="onClick"
            android:layout_marginRight="@dimen/padding3"
            android:id="@+id/r1"
            android:gravity="center"
            android:layout_width="120sp"
            android:layout_height="30sp">
            <ImageView
                android:src="@mipmap/zanred"
                android:id="@+id/iv_dianzan"
                android:paddingLeft="@dimen/padding30"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <TextView
                android:paddingRight="@dimen/padding40"
                android:id="@+id/b1"
                android:textColor="@color/gray"
                android:textSize="@dimen/text_size_10"
                android:text="點贊"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                >
            </TextView>
        </RelativeLayout>
要添加橙色的兩句,不然會被子控件截取點擊事件觸發。 代碼中Activity要實現OnclickListener接口,並複寫onClick()

	public void onClick(View view){
	      switch (view.getId()){
		 case R.id.r1:
			Toast.makeText(this,"謝謝支持",Toast.LENGTH_SHORT).show();
			break;
		case R.id.r2:
			Toast.makeText(this,"謝謝評論",Toast.LENGTH_SHORT).show();
			break;
		case R.id.r3:
			Toast.makeText(this,"更多精彩",Toast.LENGTH_SHORT).show();
			break;
	      }
	}

4.在點擊中,能用TextView的地方不要用Button,因爲Button比較笨重。

5.View之間的分割線

豎線

<View  
    Android:layout_width="1dip" 
    android:layout_height="match_parent"
    android:background="#66CCFF"
    android:layout_gravity="center_horizontal"
    />


橫線

<View  android:layout_height="1px" 
           android:layout_width="match_parent"
           android:background="#66CCFF"

/>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章