一、背景
为了强化对flowlayout流式布局的理解和简易安卓项目架构结构的理解,写一个小项目,随机选择器,控制可见等
二、项目代码
2.1流式布局
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp"
android:background="@drawable/item_background">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textSize="16sp"
/>
</LinearLayout>
2.2布局文件
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
>
<EditText
android:id="@+id/tip_title"
android:layout_width="200dp"
android:layout_height="50dp"
android:hint="@string/btn_edit"
android:gravity="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
>
</EditText>
<android.widget.Button
android:id="@+id/btn_save"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_marginStart="@dimen/margin_200"
android:layout_marginBottom="8dp"
android:background="@drawable/btn_background"
android:text="@string/btn_save"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/tip_title"></android.widget.Button>
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.internal.FlowLayout
android:id="@+id/flowLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:orientation="horizontal"
app:lineSpacing="@dimen/margin_80"
android:layout_marginTop="@dimen/margin_100"
app:layout_constraintTop_toBottomOf="@+id/tip"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
>
</com.google.android.material.internal.FlowLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_100"
app:layout_constraintTop_toBottomOf="@id/flowLayout"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
>
<android.widget.Button
android:id="@+id/btn_random"
android:layout_width="100dp"
android:layout_height="80dp"
android:textColor="@color/white"
android:background="@drawable/btn_background"
android:text="@string/btn_choose"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
>
</android.widget.Button>
<android.widget.Button
android:id="@+id/btn_clear"
android:layout_width="100dp"
android:layout_height="80dp"
android:textColor="@color/white"
android:background="@drawable/btn_background"
android:text="@string/btn_clear"
android:layout_marginStart="60dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@+id/btn_random"
>
</android.widget.Button>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_400"
app:layout_constraintTop_toBottomOf="@+id/selector"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:id="@+id/sel_show">
<TextView
android:id="@+id/sel_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sel_ans"
android:visibility="gone"
android:textColor="@color/black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
>
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sel_ans"
android:visibility="gone"
android:layout_marginStart="@dimen/margin_400"
android:background="@drawable/btn_background"
app:layout_constraintStart_toEndOf="@+id/sel_text"
app:layout_constraintTop_toTopOf="parent"
>
</TextView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
2.3 主要代码
java
package com.example.javatestwithleetcode.ui;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.javatestwithleetcode.R;
import com.example.javatestwithleetcode.utils.StringUtils;
import com.example.javatestwithleetcode.utils.ToastUtils;
import com.google.android.material.internal.FlowLayout;
import java.util.ArrayList;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private FlowLayout flowLayout;
private ArrayList<String> dataList;
private Button button;
private Button save_btn;
private EditText editText;
private Button clear_btn;
private TextView sel_ans;
private TextView sel_title;
@SuppressLint("ResourceAsColor")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initDatas();
//也可以用implements然后switch-case 也可以拆开放三个方法里
button.setOnClickListener(view -> {
Random random=new Random();
int i= random.nextInt(dataList.size());
// ToastUtils.showToast(dataList.get(i));
sel_ans.setText(dataList.get(i));
sel_ans.setVisibility(View.VISIBLE);
sel_title.setVisibility(View.VISIBLE);
});
save_btn.setOnClickListener(v->{
editText=(EditText) findViewById(R.id.tip_title);
if(StringUtils.isNotEmpty(editText.getText().toString()) ){
dataList.add(editText.getText().toString());
editText.setText("");
// 将数据添加到FlowLayout
addDataToFlowLayout(dataList);
}else{
ToastUtils.showToast("请输入后再保存");
}
});
clear_btn.setOnClickListener(v->{
dataList.clear();
flowLayout.removeAllViews();
sel_ans.setVisibility(View.GONE);
sel_title.setVisibility(View.GONE);
});
}
private void addDataToFlowLayout(ArrayList<String> dataList) {
if(dataList.isEmpty()){
return;
}
flowLayout.removeAllViews();
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, getResources().getDimensionPixelSize(R.dimen.margin_100), 0);
LayoutInflater inflater = LayoutInflater.from(this);
for (String data : dataList) {
View view = inflater.inflate(R.layout.item_flowlayout, flowLayout, false);
TextView textView = view.findViewById(R.id.textView);
textView.setText(data);
textView.setPadding(getResources().getDimensionPixelSize(R.dimen.margin_40), getResources().getDimensionPixelSize(R.dimen.margin_40)
, getResources().getDimensionPixelSize(R.dimen.margin_40), getResources().getDimensionPixelSize(R.dimen.margin_40));
textView.setSingleLine();
textView.setTextColor(getResources().getColor(R.color.white));
textView.setLayoutParams(layoutParams);
flowLayout.addView(view,layoutParams);
}
}
private void initDatas(){
setContentView(R.layout.activity_main);
dataList = new ArrayList<>();
flowLayout = findViewById(R.id.flowLayout);
sel_ans=(TextView) findViewById(R.id.sel_ans);
sel_title=(TextView) findViewById(R.id.sel_text);
sel_ans.setPadding(getResources().getDimensionPixelSize(R.dimen.margin_80),getResources().getDimensionPixelSize(R.dimen.margin_80)
,getResources().getDimensionPixelSize(R.dimen.margin_80),getResources().getDimensionPixelSize(R.dimen.margin_80));
button =(Button) findViewById(R.id.btn_random);
save_btn=(Button) findViewById(R.id.btn_save);
clear_btn=(Button) findViewById(R.id.btn_clear);
}
}
资源文件和mainfest还有工具类不作详细解释。
三、项目截图
图片放不出来。。。
四、重点
添加flowlayout,以及设置可见性,加强了对android studio项目中的textview edittext button的更深的认知,点击触发条件等,以及对constraintlayout,linerlayout和flowlayout也有了更深刻的认知