時間:2023-07-24 23:00:02 | 來源:網(wǎng)站運營
時間:2023-07-24 23:00:02 來源:網(wǎng)站運營
Android學習--制作ui界面:用Android制作一個簡單的聊天界面,大概像微信聊天那樣的界面<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#d8e0e8"> <android.support.v7.widget.RecyclerView android:id="@+id/msg_recycle_view" android:layout_height="0dp" android:layout_width="match_parent" 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 somethin here" android:maxLines="2"/> <Button android:id="@+id/send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send" /> </LinearLayout></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><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/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/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>
public class Msg { public static final int receive=0; public static final int send=1; private String content; private int type;//構(gòu)造方法 public Msg(String content,int type){ this.content=content; this.type=type; }//get方法 獲取內(nèi)容與消息類型 public String getContent(){ return content; } public int getType(){ return type; }}
public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> { private List<Msg> mMsgList;//內(nèi)部類 ViewHoleder 繼承RecyclerView.ViewHoleder public class ViewHolder extends RecyclerView.ViewHolder{ TextView leftMsg; TextView righyMsg; LinearLayout leftlayout; LinearLayout rightlayout;//ViewHolder構(gòu)造函數(shù) 傳入?yún)?shù)view (這個參數(shù)是RecyclerView 子項的最外層布局)//這樣便可以用findViewById()方法獲取linelayout和 TextView public ViewHolder(View view){ super(view); leftlayout=(LinearLayout) view.findViewById(R.id.left_layout); rightlayout=(LinearLayout)view.findViewById(R.id.right_layout); leftMsg=(TextView)view.findViewById(R.id.left_msg); righyMsg=(TextView)view.findViewById(R.id.right_msg); } }//MsgAdapter構(gòu)造函數(shù) 把數(shù)據(jù)源傳進來 賦值給新變量mMsgList 后續(xù)操作用mMsgList public MsgAdapter(List<Msg> msgList){ mMsgList=msgList; }//重寫RecyclerView.Adapter 的三個方法 @Override//這個方法是用于創(chuàng)建ViewHolder實例的 //加載 msg_item的布局 并傳入ViewHolder實例中 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 //這個方法用于對子項的數(shù)據(jù)進行操作 判斷 賦值等。 public void onBindViewHolder(ViewHolder holder, int position) { Msg msg =mMsgList.get(position); if(msg.getType()==Msg.receive){//接受消息 在左邊布局顯示 holder.leftlayout.setVisibility(View.VISIBLE); holder.rightlayout.setVisibility(View.GONE); holder.leftMsg.setText(msg.getContent()); }else if(msg.getType()==Msg.send){ //發(fā)送消息 在右邊布局顯示 holder.rightlayout.setVisibility(View.VISIBLE); holder.leftMsg.setVisibility(View.GONE); holder.righyMsg.setText(msg.getContent()); } } @Override//獲取RecycleView的子項的個數(shù) public int getItemCount() { return mMsgList.size(); }}
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); //初始化消息數(shù)據(jù) initMsgs(); //創(chuàng)建 輸入框與發(fā)送按鈕 的實例化對象 inputText = (EditText) findViewById(R.id.input_text); send = (Button) findViewById(R.id.send); //創(chuàng)建 RecyclerView 滾動控件的實例化對象 msgRecyclerView = (RecyclerView) findViewById(R.id.msg_recycle_view); //RecyclerView設置 LineLayout 的布局方式 LinearLayoutManager layoutManager = new LinearLayoutManager(this); msgRecyclerView.setLayoutManager(layoutManager); //RecyclerView設置適配器 并將消息的數(shù)據(jù)傳入適配器中 實現(xiàn)和數(shù)據(jù)的關(guān)聯(lián) adapter = new MsgAdapter(msgList); msgRecyclerView.setAdapter(adapter); //按鈕點擊事件 send.setOnClickListener(new View.OnClickListener() { @Override //按鈕點擊發(fā)送消息 public void onClick(View view) { String content =inputText.getText().toString(); if(!"".equals(content)){ Msg msg=new Msg(content,Msg.send); msgList.add(msg); adapter.notifyItemInserted(msgList.size()-1); //當有新消息時 刷新ListView中的顯示 msgRecyclerView.scrollToPosition(msgList.size()-1);//將ListView 定位到最后一行 inputText.setText("");//清空輸入框的內(nèi)容 } } }); } //初始的三條消息 private void initMsgs() { Msg msg1 = new Msg("Hello .", Msg.receive); msgList.add(msg1); Msg msg2 = new Msg("Hello who are you? ", Msg.send); msgList.add(msg2); Msg msg3 = new Msg("haha This is stinglog", Msg.receive); msgList.add(msg3); }}
最終的效果大概是這樣的,我的也實現(xiàn)了 但是消息的背景圖片選的太丑了,就不在此獻丑了。關(guān)鍵詞:界面,學習
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。