Learning experience of the first line of code (Android) - activity

UI related knowledge

1, Four layouts

LinearLayout

Android: orientation specifies the arrangement. Vertical is vertical and Horizontal is Horizontal.
Android: layout > gravity specifies the alignment of the control in the layout, top,bottom,center. If the arrangement is Horizontal, it can only be changed in the vertical direction.

A little Chestnut:
<EditText Android:width="0dp" Android: layout_weight="1" />
<Button Android:width="0dp" Android: layout_weight="1" />
They will now divide the screen width horizontally.

RelativeLayout

Android:layout_alignParentBottom="true", Android:layout_alignParentRight="true"Android:layout_alignParentLeft="true", Android:layout_alignParentTop="true"
Android:layout_centerInParent="true"
Make the component in the top, bottom, left and right of the parent layout respectively, or specify the component in the middle for a component as a location point. Use Android: layout? Centerinparent = "true".
Android:llayout_toRightof = "true, similarly to leftof, about, below"

FrameLayout

All components are placed in the top left corner of the layout by default. You can use Android: layout? Gravity to specify the alignment of controls in the layout, left,right

PercentLayout

You need to add the implementation 'com.android.support:percent:27.0.2' in app/gradle. Note that the tag should use the full package name android.support.percent.PercentRelativeLayout.
App: layout ﹐ widthpercentage =% 50, app: layout ﹐ highpercentage =% 50, 50% width and 50% height respectively.

2, Component use

button.setVisibility(… )Set whether the component is visible. There are three properties: view.gone (remove), View.VISIBLE,View.INVISIBLE (transparent and invisible)

Common components Special components Custom components
Button ListView Introduction layout
TextView RecyclerView Create custom control
EditText
ImageView
ProgressBar
AlertDialog
ProgressDialog

Common components:

Button

Android:textAllCaps="false" disable automatic uppercase conversion.
Register listener:
1.

Button button=(Button) findViewById(R.id.buttonname)
button.setonClickListener(new View.OnclickListener){
		public void onClick(View v){
		}
}

2...mainactivity ...implements View.OnclickListener{

public void onCreate(){
Button button=(Button) findViewById(R.id.buttonname)
button.setonClickListener(this);}
public void onClick(View v){
switch(v.getID()){
	case R.id.button{
		}//Add logic
	}
	....
}

EditText

android:hint = / / add hidden text and prompt text.
android:maxlines = / / specifies the maximum number of lines.

editText.getText().toString();//Gets the text in the edit bar and converts it to a string.

TextView

Android: textsize specifies the text size
android:textcolor specifies the text color

ImageView

android:src = "" specify picture path

ImageView imageview=(Imageview) findViewById(R.id.ImgaeId);//Working with xml attributes
imageview.setImageResource(R.id.ImageId);//Can be set by image path

ProgressBar

The default is circle progress bar, style="?android:attr/progressBarStyleHorizontal" / / set as horizontal progress bar

int progress=progress.getProgressBar();
progress+=10;//You can use this similar method to control the progress of the progress bar

AlertDialog

Placed on top of all interface elements, it can shield the interaction ability of other components and serve as a confirmation box.

setPosiveButton() sets the click event for the OK button for the dialog box.

ProgressDialog

The usage is similar to AlertDialog.

Public void onClick(
	switch(v.getId()){
		case R.id.button:
		ProgressDialog progressdialog=new ProgressDialog(MainActivity.this);
		progressdialog.setTitle(" Title");
		progressdialog.setMassage("Loading ...");
		progressdialog.setCancelable(true);//Pass in true to cancel through Back, and pass in false to cancel
		progressdialog.show();
		break;//After loading, you should close the dialog box with the loss () method
{

Special components (most common but difficult):

ListView

public class FruitAdapter extends ArrayAdapter<Fruit> {
    private int resourceId;
    public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects){
        super(context,textViewResourceId,objects);
        resourceId=textViewResourceId;
        }
    public View getView(int position, View convertView, ViewGroup parent)
    {
        Fruit fruit=getItem(position);
        View view;
        ViewHolder viewHolder;
        if(convertView==null){
            view=LayoutInflater.from(getContext()).inflate(resourceId,parent,false);//Used to load layout
            viewHolder=new ViewHolder();
            viewHolder.fruitimage=(ImageView) view.findViewById(R.id.fruit_image);
            viewHolder.fruitname=(TextView) view.findViewById(R.id.fruit_name);
            view.setTag(viewHolder);//Save ViewHolder in View
        }else {
            view=convertView;
            viewHolder=(ViewHolder) view.getTag();//Get ViewHolder again
        }
        viewHolder.fruitimage.setImageResource(fruit.getImageId());
        viewHolder.fruitname.setText(fruit.getName());//Set up picture and text bar display
        return view;
    }
    class ViewHolder{
        ImageView fruitimage;
        TextView fruitname;
    }

public class Fruit {
    private String name;
    private int imageId;
    public Fruit(String name,int imageId){this.name=name; this.imageId=imageId; }
    public int getImageId() {return imageId; }
    public String getName() {return name;}
}

//Click events
public class MainActivity extends AppCompatActivity {
    private List<Fruit> fruitList=new ArrayList<>();
    protected void onCreate(Bundle savedInstanceState) {
        /*super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);*/
        FruitAdapter adapter=new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList);
        ListView listView=(ListView) findViewById(R.id.list_view);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {//Set up a listener
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Fruit fruit=fruitList.get(position);
                Toast.makeText(MainActivity.this,fruit.getName(),Toast.LENGTH_SHORT).show();
            }
        });
    }

RecyclerView

1. configuration
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
2. Use (see article separately)

Introduction layout

Add this sentence to the xml file:
<include layout=""@layout_id"

Published 1 original article · praised 0 · visited 4
Private letter follow

Tags: Android xml Gradle

Posted on Tue, 11 Feb 2020 08:34:10 -0500 by waygood