Preface
Because the wifi is broken at home during the summer vacation, I think I've been studying Android for so long that I haven't written even a simple app of my own, so I spent some time doing it. I remember when I started to learn programming, some buddies in the learning group sent out their own notebook software, so I'll make a notebook app this time.
Preparing knowledge
- Android Studio (v2.3)
- ListView
- SQLite
- File store
- Custom view
Let's get started.
It is not difficult as a whole to customize Toast and add a picture display to the Toast prompt window. I found a blog of custom Toast on the internet, a lot of code, which is troublesome. So I looked at Toast's document and found a setView() method:
My understanding is to make a layout and pass it on to this method, so that you can achieve what you want.
public class ToastView{
private static TextView toastText;
private static ImageView toastImageView;
public static void showToast(Context context, String message, Drawable image){
Toast toast = new Toast(context);
View toastView = LayoutInflater.from(context).inflate(R.layout.toast_view,null);
toastText= (TextView) toastView.findViewById(R.id.toast_text);
toastText.setText(message);
toastText.setTextSize(26);
toastImageView= (ImageView) toastView.findViewById(R.id.toast_image_view);
if(image!=null){
toastImageView.setImageDrawable(image);
}
toast.setView(toastView);
toast.show();
}
public static void showToast2(Context context, String message, int image){
Toast toast = new Toast(context);
View toastView = LayoutInflater.from(context).inflate(R.layout.toast_view,null);
toastText= (TextView) toastView.findViewById(R.id.toast_text);
toastText.setText(message);
toastText.setTextSize(26);
toastImageView= (ImageView) toastView.findViewById(R.id.toast_image_view);
toastImageView.setImageResource(image);
toast.setView(toastView);
toast.show();
}
}
There are two ways to implement custom view. The main reason is that the parameters of the incoming image are different. showToast() is to import a Drawable object and showToast2() is the id of the incoming image resource.
The following is the layout of the custom Toast:
<!-- toast_view.xml -->
<?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:background="@drawable/toast_background"
android:orientation="horizontal">
<ImageView
android:id="@+id/toast_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="15dp"
android:src="@mipmap/ic_touxiang" />
<TextView
android:id="@+id/toast_text"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="15dp"
android:layout_height="wrap_content"
android:textColor="#000000" />
</LinearLayout>
After each edit is completed, a uuid will be generated as the file name of the edited content stored in memory. At the same time, uuid will be saved to the database to facilitate the creation of note lists. Listview reads the corresponding files by acquiring the uuid in the database, deleting the item of listview will delete the corresponding files of uuid and uuid in the database (these are very simple, that is, to say, to delete the corresponding files of uuid and uuid in the database). Let's not go into details.
But while deleting item, deleting uuid in the database is stuck here, so we have to add a TextView to each custom item layout to set the content of this TextView as uuid of the corresponding file for each item.
<!-- listview_item.xml -->
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">
<TextView
android:id="@+id/list_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="20sp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/guideline"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="@+id/guideline2"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintTop_toTopOf="@+id/imageView2"
android:layout_marginTop="0dp" />
<TextView
android:id="@+id/un_visiable"
android:visibility="gone"
android:layout_width="0dp"
android:layout_height="0dp"
/>
<TextView
android:id="@+id/list_content"
android:textColor="#000000"
android:textSize="15sp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:maxLines="1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="@+id/guideline2"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintBottom_toBottomOf="@+id/imageView2"
android:layout_marginBottom="0dp" />
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guideline"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5" />
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guideline2"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.21614583" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintRight_toLeftOf="@+id/guideline2"
android:layout_marginRight="8dp" />
</android.support.constraint.ConstraintLayout>
Finally, a Floating Action Button was added, which was useless, just for the sake of pretension, and MainActivity had a side-slip menu which was very painful, but it was useless.
github address: https://github.com/killerYe/MyApp
Novice goes on to say, no blogging experience, no development experience, where do not do well, just give advice, don't spray oh!!
Because I'm just learning android soon, so interested students can add a Wechat, study together and discuss it!!!