控件使用問題

TextView最大長度限制,超出部分省略號顯示

xml中屬性設置:

 

<TextView  
	android:id="@+id/name"  
	android:layout_width="wrap_content"  
	android:layout_height="wrap_content"  
	android:ellipsize="end"  
	android:maxEms="7"  
	android:singleLine="true"  
	android:textColor="@color/text_deep_black_color"  
	android:textSize="15sp" /> 

 其中關鍵參數:

android:ellipsize="end"  

android:maxEms="7"  

android:singleLine="true"  

 

PS:設置maxLength達不到這個效果,maxLength不會自動顯示省略號。

 

 

 

LinearLayout中組件右對齊

在LinearLayout中,如果將其定位方向設爲橫向排列:android:orientation="horizontal",那麼這個佈局中的控件將自左向右排列。

但有時會有這樣的情況:行的左邊有兩個控制的同時,行的右邊也有一個控制。

這怎麼處理呢?

我們可以將右邊的控件放在另一個LinearLayout中,同時將其對齊方式設爲右對齊:android:gravity="right",還有一點,這個LinearLayout的寬度設爲充滿父控件: android:layout_width="fill_parent"。這樣就行了。

 

完整的XML代碼如下:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:background="@drawable/bg"  
    android:orientation="horizontal" >  
  
    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="左邊1" />  
  
    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="左邊2" />  
    <!-- 將TextView包在另一個LinearLayout中  
         注意android:layout_width和android:gravity這兩個屬性  
     -->  
    <LinearLayout  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:gravity="right" >  
  
        <TextView  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_marginRight="10dp"  
            android:text="右邊" />  
    </LinearLayout>  
  
</LinearLayout> 

 

 

 android添加橫線或豎線

橫線:

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

 

豎線:

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

 

 Activity與Fragment之間的跳轉:

 

(1)Fragment跳轉到Activity:

startActivity(new Intent(getActivity(), Activity.class)); // 首先需要通過getActivity()方法獲取到當前Activity

(2)Activity跳轉到Fragment:

// Activity不能直接跳轉到Fragment,需要先跳轉到Fragment所附着的Activity中,然後再更改Activity當前顯示哪個Fragment。

intert.putExtra("fragid",1); //首先在Activity跳轉之前,在Intent中傳入一個flag,用來標記跳轉到哪一個Fragment。

// 然後根據flag來判斷顯示哪個Fragment

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); // import android.support.v4.app.FragmentTransaction;

transaction.replace(R.id.frame_content, new CenterFragment());

transaction.commit();

 

 

RelativeLayout 顯示、隱藏、移除

RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative);

1)通過下面代碼實現顯示相對佈局的內容

    layout.setVisibility(View.VISIBLE);

2)通過下面代碼移除顯示相對佈局的內容

    layout.setVisibility(View.GONE);

3)通過下面代碼隱藏顯示相對佈局的內容

layout.setVisibility(View.INVISIBLE);

 

setVisible(VIew.GONE);  //隱藏 並且不佔用界面空間

setVisible(VIew.VISIBLE);  //控件顯示

 

setVisible(VIew.INVISIBLE);  //控件隱藏 佔用界面空間

 

 

android代碼設置RelativeLayout的高度

private RelativeLayout relative;
relative = (RelativeLayout) findViewById(R.id.rlay_1);
ViewGroup.LayoutParams params=relative.getLayoutParams();
params.height =180; 
relative.setLayoutParams(params);

  

ImageView.ScaleType:

CENTER /center  按圖片的原來size居中顯示,當圖片長/寬超過View的長/寬,則截取圖片的居中部分顯示

CENTER_CROP / centerCrop  按比例擴大圖片的size居中顯示,使得圖片長(寬)等於或大於View的長(寬)

CENTER_INSIDE / centerInside  將圖片的內容完整居中顯示,通過按比例縮小或原來的size使得圖片長/寬等於或小於View的長/寬

FIT_CENTER / fitCenter  把圖片按比例擴大/縮小到View的寬度,居中顯示

FIT_END / fitEnd   把圖片按比例擴大/縮小到View的寬度,顯示在View的下部分位置

FIT_START / fitStart  把圖片按比例擴大/縮小到View的寬度,顯示在View的上部分位置

FIT_XY / fitXY  把圖片不按比例擴大/縮小到View的大小顯示

 

MATRIX / matrix 用矩陣來繪製,動態縮小放大圖片來顯示。

 

layout_centerInParent與layout_centerHorizontal與layout_gravity的區別

是相對於RelativeLayout

android:layout_centerInParent="true"  --將控件置於父控件的中心位置

android:layout_centerHorizontal="true"  --該控價設置在相對於父控件水平居中的位置

android:layout_centerVertical="true"  --將控件置於垂直方向的中心位置

 

android:layout_alignParentLeft="true"

android:layout_alignParentRight="true"

android:layout_alignParentTop="true"

android:layout_alignParentBottom="true

android:layout_centerInParent="true"

 

layout_gravity

針對LinearLayout的一種控件對齊方式,可以把值設置成下列值:

center_vertical、center_horizontal、center等等

 

gravity

 

控制控件內文字的對齊方式

 

RadioGroup的RadioButton中文字和圖片的距離

android:button=@null;//將默認的button圖片清空

android:drawableLeft=@drawable/radiobutton;//使用該屬性定義button圖片

android:background=@null;//將radioButton的背景設爲空

 

android:drawablePadding=6dp;//將文字和左側的button圖片相距6dp

 

給LinearLayout設置點擊事件

方法一:

1,在代碼中加入如下代碼,不然會被包含在其中的控件把焦點搶佔,此時子控件無需設置clickable和focuseable

<RelativeLayout
            android:id="@+id/relativeLayout"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_weight="1"
			
            android:onClick="onClick"
            android:clickable="true" >

            ......
</RelativeLayout>

 

2,一方面可以在代碼中find出來,添加監聽器onClickListenner,另一方面也可以添加一個onClick()方法,在activity中實現指定的方法。以後者爲例。在佈局中必須加上代碼android:onClick="onClick",其中等號右邊的值,可以自定義爲自己的方法,只是得在activity中提供此方法,類似onClick()。

3,activity的添加代碼,如下

 

public void onClick(View v) {
        switch (v.getId()){
            case R.id.relativeLayout:
                System.out.println("整個佈局被點擊");
                break;
        }
    }

 

方法二:

<RelativeLayout
            android:id="@+id/relativeLayout"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_weight="1"
			
            android:focusableInTouchMode="true" >
            ......
</RelativeLayout>

 

1,在activity中find在添加OntouchLisenner方法

2,實現OntouchLisenner的Ontouch()方法

 

可能出現的問題

 

1,當把<RelativeLayout></RelativeLayout>嵌套在其他佈局時,android:focusableInTouchMode="true"和android:clickable="true"不能同時設置。

 

2,當有多個<RelativeLayout></RelativeLayout>實現整體點擊時,應當讓activity實現OnClickListenner或OntouchListenner,不要單獨的在給佈局設置時new,即不要給每個佈局setOnTouchListener(new ...);每個監聽器都是新的很容易出現執行多次監聽方法

 

 

判斷imageview是否有圖片或者是默認圖片

Drawable.ConstantState frontState = iv_front.getDrawable().getCurrent().getConstantState();
if (frontState==null){
    AppContext.showToast("請上傳圖片");
    return;
}

 

 

。。。

發佈了701 篇原創文章 · 獲贊 67 · 訪問量 39萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章