Android 自定義View-----流式佈局(粗糙實現)
阿新 • • 發佈:2018-11-16
//首先檢視一下佈局介面吧
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <EditText android:id="@+id/ed_text" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" /> <Button android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="新增" /> </LinearLayout> <soexample.umeng.com.liushibuju.MyViewLS android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/selfView"></soexample.umeng.com.liushibuju.MyViewLS> </LinearLayout>
//很簡單一個輸入框 一個按鈕
//MainActivity介面
//尋找控制元件啥的就不說了
package soexample.umeng.com.liushibuju; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.Toast; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private EditText td_text; private ArrayList<String> list = new ArrayList<>( ); private MyViewLS self; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); setContentView( R.layout.activity_main ); td_text = findViewById( R.id.ed_text ); self = findViewById( R.id.selfView ); findViewById( R.id.add ).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { String trim = td_text.getText().toString().trim(); if (TextUtils.isEmpty( trim )){ Toast.makeText( MainActivity.this, "內容不可為空", Toast.LENGTH_SHORT ).show(); } list.add( trim ); //這裡要傳一個存放輸入的值的集合 self.setListData(list); } } ); } }
//自定義控制元件的類
package soexample.umeng.com.liushibuju; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; import java.util.ArrayList; import java.util.List; /* 流式佈局 繼承RelativeLayout 實現三個構造方法 * */ public class MyViewLS extends RelativeLayout { private List<String> listData; private LinearLayout view_v; private LinearLayout view_h; private TextView textView; private View view1; private String data; private int position = 0; public MyViewLS(Context context) { super( context ); init( context ); } public MyViewLS(Context context, AttributeSet attrs) { super( context, attrs ); init( context ); } public MyViewLS(Context context, AttributeSet attrs, int defStyleAttr) { super( context, attrs, defStyleAttr ); init( context ); } private Context context; private void init(Context context) { this.context = context; //在init方法裡面找到垂直的佈局 記得強轉為LinearLayout view_v = (LinearLayout) View.inflate( context, R.layout.layout_v, null ); //把垂直的佈局新增到佈局裡面 addView( view_v ); } private void setlist() { //記得清空控制元件 view_v.removeAllViews(); view_h = (LinearLayout) View.inflate( context, R.layout.layout_h, null ); //找到水平的佈局 新增到垂直的佈局裡面 view_v.addView( view_h ); view_h.removeAllViews(); //記得清空 int len = 0; for (int i = 0; i < listData.size(); i++) { position = i; data = listData.get( i ); len += data.length(); if (len > 28) { view_h = (LinearLayout) View.inflate( context, R.layout.layout_h, null ); view_v.addView( view_h ); len = 0; } view1 = View.inflate( context, R.layout.layout_text, null ); textView = view1.findViewById( R.id.text_name ); textView.setText( data ); view_h.addView( view1 ); //長按刪除 textView.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { view_v.removeView(view1); listData.remove(position); setlist(); return true; } }); } /*textView.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { Toast.makeText( context, textView.getText().toString().trim(), Toast.LENGTH_SHORT ).show(); } } );*/ } public void setListData(ArrayList<String> listData) { this.listData = listData; if (!listData.isEmpty()) { setlist(); } } }
//三個佈局 逐次新增
//水平佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
</LinearLayout>
//垂直佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
//文字佈局
//文字的佈局寬度記得改為match_parent
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:background="@drawable/shape"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_name"/>
</LinearLayout>