1. 程式人生 > >Android學習筆記——UI基礎之編寫介面最佳實踐

Android學習筆記——UI基礎之編寫介面最佳實踐

參考書籍:Android第一行程式碼(第二版).郭霖著

1、製作Nine-Patch圖片
一種被特殊處理過的png圖片,能夠指定那些區域可以被拉伸、哪些不可以。在Android sdk目錄下有一個tools資料夾,找到draw9patch.bat檔案來製作Nine-Patch圖片(要開啟此檔案需將JDK的bin目錄配置到環境變數中,如使用的AS內建jdk,則要配置的路徑是Android Studio安裝目錄/jre/bin).
由於Android Studio已內建此功能,所以只需將圖片匯入專案後點擊滑鼠右鍵->Create 9-Patch file…然後確認路徑名稱即可。

2、編寫精美聊天介面
需要準備接收和傳送訊息背景圖(nine-patch),使用RecyclerView控制元件,所以要在build.gradle中新增相應依賴庫
compile ‘com.android.support:recyclerview-v7:24.2.1’
編寫主介面,修改佈局檔案:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/msg_recycler_view"
        android:layout_width
="match_parent" android:layout_height="0dp" android:layout_weight="1"/>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/input_text" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_weight="1" android:hint="Type something here" android:maxLines="2"/>
<Button android:id="@+id/send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send"/> </LinearLayout> </LinearLayout>

中間顯示聊天訊息內容,底部為文字輸入框和傳送按鈕。
然後定義訊息實體類:

public class Msg {
    public static final int TYPE_RECEIVED = 0;
    public static final int TYPE_SENT = 1;
    private String content;
    private int type;
    public Msg(String content, int type){
        this.content = content;
        this.type = type;
    }

    public String getContent() {
        return content;
    }

    public int getType() {
        return type;
    }
}

content表示訊息內容,type表示訊息型別(兩個值可選:TYPE_RECEIVED/TYPE_SENT).
編寫RecyclerVeiw子項佈局,新建msg_item.xml如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp">

    <LinearLayout
        android:id="@+id/left_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:background="@drawable/message_left">

        <TextView
            android:id="@+id/left_msg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:textColor="#fff"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/right_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:background="@drawable/message_right">

        <TextView
            android:id="@+id/right_msg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp" />

    </LinearLayout>

</LinearLayout>

接收訊息居左對齊,傳送訊息居右對齊,分別設定了背景圖。後面通過程式碼來顯示隱藏傳送/接受訊息。
接下來建立RecyclerView的介面卡類,新建類MsgAdapter:

public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {
    private List<Msg> mMsgList;
    static class ViewHolder extends RecyclerView.ViewHolder{
        LinearLayout leftLayout;
        LinearLayout rightLayout;
        TextView leftMsg;
        TextView rightMsg;

        public ViewHolder(View itemView) {
            super(itemView);
            leftLayout = (LinearLayout) itemView.findViewById(R.id.left_layout);
            rightLayout = (LinearLayout) itemView.findViewById(R.id.right_layout);
            leftMsg = (TextView) itemView.findViewById(R.id.left_msg);
            rightMsg = (TextView) itemView.findViewById(R.id.right_msg);
        }
    }
    public  MsgAdapter(List<Msg> msgList){
        mMsgList = msgList;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Msg msg = mMsgList.get(position);
        if (msg.getType() == Msg.TYPE_RECEIVED){
            //如果是收到的訊息,則顯示左邊的訊息佈局,將右邊的訊息佈局隱藏
            holder.leftLayout.setVisibility(View.VISIBLE);
            holder.rightLayout.setVisibility(View.GONE);
            holder.leftMsg.setText(msg.getContent());
        }else if(msg.getType() == Msg.TYPE_SENT){
            //如果是發出的訊息,則相反的處理
            holder.rightLayout.setVisibility(View.VISIBLE);
            holder.leftLayout.setVisibility(View.GONE);
            holder.rightMsg.setText(msg.getContent());
        }

    }

    @Override
    public int getItemCount() {
        return mMsgList.size();
    }
}

最後修改MainActivity中的程式碼,初始化資料並給傳送按鈕加入事件響應:

public class MainActivity extends AppCompatActivity {
    private List<Msg> msgList = new ArrayList<>();
    private EditText inputText;
    private Button send;
    private RecyclerView msgRecyclerView;
    private MsgAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initMsgs();//初始化訊息資料
        inputText = (EditText) findViewById(R.id.input_text);
        send = (Button) findViewById(R.id.send);
        msgRecyclerView = (RecyclerView) findViewById(R.id.msg_recycler_view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        msgRecyclerView.setLayoutManager(layoutManager);
        adapter = new MsgAdapter(msgList);
        msgRecyclerView.setAdapter(adapter);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String cotent = inputText.getText().toString();
                if (!"".equals(cotent)){
                    Msg msg = new Msg(cotent,Msg.TYPE_SENT);
                    msgList.add(msg);
                    adapter.notifyItemInserted(msgList.size() - 1);//當有新訊息時,重新整理RecyclerView中的顯示
                    msgRecyclerView.scrollToPosition(msgList.size() - 1);//將RecyclerView定位到最後一行
                    inputText.setText("");//清空輸入框
                }
            }
        });

    }
    private void initMsgs(){
        Msg msg1 = new Msg("Hello guy." , Msg.TYPE_RECEIVED);
        msgList.add(msg1);
        Msg msg2 = new Msg("Hello.Who is tha?" , Msg.TYPE_SENT);
        msgList.add(msg2);
        Msg msg3 = new Msg("This is Tom.Nice talking to you." , Msg.TYPE_RECEIVED);
        msgList.add(msg3);

    }
}

效果如下:
聊天介面