Android之原始的QQ好友分組實現

來源:互聯網
上載者:User

廢話不多說,先看下效果:

 

 

 

這個就是類似抽屜的效果,這邊做了三個抽屜,點擊抽屜既可開啟,同時關閉其他抽屜。

 

有人猜到怎麼做的了嗎?

 

其實很簡單,就是三個 TextView + 三個Layout。 關鍵就在於控制Layout的顯示、消失。同時也要注意Layoout的權重值weight。

 

下面看一下代碼吧。

 

頁面 main.xml :

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:id="@+id/tv01" android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="@string/label1"android:background="@drawable/line" android:clickable="true" /><LinearLayout android:id="@+id/layout1"android:layout_width="fill_parent" android:layout_height="wrap_content"android:layout_weight="1"><TextView android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="內容1" /></LinearLayout><TextView android:id="@+id/tv02" android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="@string/label2"android:background="@drawable/line" android:clickable="true" /><LinearLayout android:id="@+id/layout2"android:layout_width="fill_parent" android:layout_height="wrap_content"android:layout_weight="1"><TextView android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="內容2" /></LinearLayout><TextView android:id="@+id/tv03" android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="@string/label2"android:background="@drawable/line" android:clickable="true" /><LinearLayout android:id="@+id/layout3"android:layout_width="fill_parent" android:layout_height="fill_parent"android:layout_weight="0"><TextView android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="內容3" /></LinearLayout></LinearLayout>

Java代碼,就一個類:

package com.yfz;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.LinearLayout;import android.widget.TextView;public class QQ extends Activity {private TextView tv1;private TextView tv2;private TextView tv3;private LinearLayout layout1;private LinearLayout layout2;private LinearLayout layout3;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        setupView();    }private void setupView() {tv1 =(TextView) findViewById(R.id.tv01);tv2 =(TextView) findViewById(R.id.tv02);tv3 =(TextView) findViewById(R.id.tv03);layout1 = (LinearLayout)findViewById(R.id.layout1);layout2 = (LinearLayout)findViewById(R.id.layout2);layout3 = (LinearLayout)findViewById(R.id.layout3);tv1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {layout1.setVisibility(View.VISIBLE);layout2.setVisibility(View.GONE);layout3.setVisibility(View.GONE);}});tv2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {layout1.setVisibility(View.GONE);layout2.setVisibility(View.VISIBLE);layout3.setVisibility(View.GONE);}});tv3.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {layout1.setVisibility(View.GONE);layout2.setVisibility(View.GONE);layout3.setVisibility(View.VISIBLE);}});}}

 

Demo程式中考慮將抽屜都放在上面,所以最後一個Layout的權重最高:android:layout_weight="0" ;

另外注意,Layout的android:layout_height屬性都必須是wrap_content 。 用fill_parent就沒戲啦!

 

如果有人想要講抽屜放在最下面,那麼局部檔案需要小改一下。 

1. 換一下TextView 和 Layout的位置

2. 講最上面的Layout的權重設為最高

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.