2012年6月17日 星期日

ListView 圓角化







準備一個箭頭的.png 檔

main.java:


public class ListViewCorneredBgActivity extends Activity {
private ListView mListView = null;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ArrayList<String> list =new ArrayList<String>();
        list.add("1. 台北 (Taipei)");
        list.add("2. 台中 (Taichung)");
        list.add("3. 台南 (Tainan)");
        list.add("4. 基隆 (Keelung)");
        list.add("5. 台東 (Taitung)");
        list.add("6. 高雄 (Kaohsiung)");
        mListView =  (ListView)findViewById(R.id.list);
        mListView.setAdapter(new AppAdapter(this,list));
    }
    
    class AppAdapter extends BaseAdapter implements ListAdapter{

        Context context;
        ArrayList<String> list;
        AppAdapter(Context context,ArrayList<String> list){
            this.context=context;
            this.list=list;
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            TextView tv;
            if(convertView==null){
                convertView=LayoutInflater.from(context).inflate(R.layout.simple_item_2, null);
            }
            tv=(TextView)convertView.findViewById(R.id.name);
            tv.setText(list.get(position));
            return convertView;
        }
        
    }
}

*********************************************************
res/layout/main.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:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"
         />
    <ListView android:id="@+id/list" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_margin="20.0dip"
        android:cacheColorHint="@null" 
                android:listSelector="@drawable/shape_bg_listview"
        android:background="@drawable/shape_bg_listview"
        />

</LinearLayout>

res/layout/simple_item_2.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    <TextView
        android:id="@+id/name"
        android:textSize="17.0sp"
        android:textColor="@android:color/black"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20.0dip"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        >
    </TextView>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="20.0dip"
        android:src="@drawable/arrowright1"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        >
    </ImageView>
</RelativeLayout>

***********************
res/drawable/shape_bg_listview.xml::

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
       android:shape="rectangle"
       >
       <!--  
       <gradient  
       android:startColor="@android:color/white"  
       android:endColor="#FFCCCCCC"  
       android:angle="180" 
       />
        -->
          
       
       <stroke android:width="0px" 
       android:color="@android:color/white" 
       /> 
       <solid android:color="@android:color/white" 
       /> 
       <corners 
       android:bottomRightRadius="20px" 
       android:bottomLeftRadius="20px" 
       android:topLeftRadius="20px" 
       android:topRightRadius="20px" 
       />
</shape>


參考網站:http://www.cnblogs.com/shanzei/archive/2012/03/24/2415887.html




Context Menu

使用 CONTEXT MENU




在 onCreate 裡加上:


public void onCreate(Bundle savedInstanceState) {


registerForContextMenu(getExpandableListView());
}

然後加上:

 @Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
//super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderIcon(R.drawable.heart);
    menu.setHeaderTitle("親愛的 Tom");
    menu.add(0,0,0,R.string.hello);
}

Spannable 文章的每一個字顯示其義


希望按下一段英文文章的每一個字都能顯示其意思。

利用 Spannable 和 ClickableSpan




public class Cq_Spannable01Activity extends Activity {
    /** Called when the activity is first created. */

private TextView tv;
//private ListView lv;
private static Context ctx;

private String str = "I love you, but she doesn't love him.";
private String [] strings;

private String result = ""; //每個元素 加上 href 之後

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
   /** Test the displaying of the components in a string **/
       // lv = (ListView) findViewById(R.id.listView1);
        strings = TextUtils.split(str, " ");
        //clear the empty space
        for (int i = 0; i< strings.length; i++) {
        String temp = "";
        String temp02 = "";
        temp = strings[i].trim();
        if (temp.endsWith(".") || temp.endsWith(",")){
        temp02 = temp.substring(0, temp.length()-1);
        temp = temp02;
       
        }
        temp = "<a style='color:yellow' href='" + temp + "'> " + temp + "</a>";
       
        strings[i] = temp;
        }
     
//        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings);
//        lv.setAdapter(adapter);
        /** Test the displaying of the components in a string **/
     
     
        for (int i = 0; i<strings.length; i++) {
        result += strings[i];
        }
     
     
     
     
        tv = (TextView) findViewById(R.id.textview);
        tv.setTextSize(25);
        ctx = this;
     
        String htmlLinkText = "";
//        htmlLinkText = "aaaaaaaaaa"
//         + "<a style='color:red' href='我是超連結...'>請按超連結</a>"
//         + "aaaaaaaaaaaaaaaa";
     
     
     
        htmlLinkText = "aaaaaaaaaaa"
        + "<a style='color:yellow' href='我'> I</a>"
        + "<a style='color:red' href='愛'> love</a>"
        + "<a style='color:green' href='妳'> you. </a>"
        + "aaaaaaaaaaaaaaaa";
     
     
     
     
        //tv.setText(Html.fromHtml(htmlLinkText));
        tv.setText(Html.fromHtml(result));
        tv.setMovementMethod(LinkMovementMethod.getInstance());
     
        CharSequence text = tv.getText();
        if (text instanceof Spannable) {
        int end = text.length();
        Spannable sp = (Spannable) tv.getText();
        URLSpan[] urlspan = sp.getSpans(0, end, URLSpan.class);
       
        SpannableStringBuilder style = new SpannableStringBuilder(text);
        style.clearSpans();
        for (URLSpan url : urlspan) {
        MyURLSpan myURLSpan = new MyURLSpan(url.getURL());
        style.setSpan(myURLSpan, sp.getSpanStart(url), sp.getSpanEnd(url), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
       
        tv.setText(style);
        }
     
    }
 
 
    private static class MyURLSpan extends ClickableSpan {
    private String mUrl;
    private String temp;
   
    MyURLSpan(String url) {
    mUrl = url;
   
    }

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

//widget.setBackgroundColor(Color.parseColor("#00000000"));

if (mUrl.equalsIgnoreCase("I")) {
mUrl = "我";
}else if (mUrl.equalsIgnoreCase("love")) {
mUrl = "愛";

}else if (mUrl.equalsIgnoreCase("you")) {
mUrl = "妳";
}else if (mUrl.equalsIgnoreCase("but")) {
mUrl = "但是";
}else if (mUrl.equalsIgnoreCase("she")) {
mUrl = "她";
}else if (mUrl.equalsIgnoreCase("doesn't")) {
mUrl = "不";
}else if (mUrl.equalsIgnoreCase("him")) {
mUrl = "他";
}

Toast.makeText(ctx, mUrl, Toast.LENGTH_SHORT).show();
}
   
    } //end of class myURLSpan
}