Android的几个基本控件和布局

2017-04-27 • AndroidNo Comments

基本控件:

1. TextView

<TextView
    android:id="@+id/text_view_a"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Hello World!"
    android:textColor="#990"
    android:textSize="24sp" />

textView_a.setText("Change World!");

2. Button

<Button
    android:id="@+id/button_a"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Click Me!"
    android:textAllCaps="false" />

3. EditText

<EditText
    android:id="@+id/edit_text_a"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Please input your name"
    android:maxLines="1" />

String string = edit_text_a.getText().toString();

4.ImageView

<ImageView
    android:id="@+id/image_view_a"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:layout_weight="1"
    android:src="@drawable/chino" />

image_view_a.setImageResource(R.drawable.timg);

5. ProgressBar

<ProgressBar
    android:id="@+id/progressbar_a"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="100" />

if (progress_bar_a.getVisibility() == View.VISIBLE) {
    progress_bar_a.setVisibility(View.GONE);
} else {
    progress_bar_a.setVisibility(View.VISIBLE);
    progress_bar_a.setProgress(60);
}

6. ProgressDialog

progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("FLY MISSION");
progressDialog.setMessage("Shall we go?");
progressDialog.setCancelable(true);
progressDialog.show();

progressDialog.dismiss();

7. AlterDialog

AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("Good night");
dialog.setMessage("Shall we play?");
dialog.setCancelable(true);
dialog.setPositiveButton("PLAY", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

    }
});

dialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

    }
});

dialog.show();

布局

1. LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/image_view_a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_weight="1"
        android:src="@drawable/chino" />

    <ImageView
        android:id="@+id/image_view_b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_weight="1"
        android:src="@drawable/timg" />

</LinearLayout>

2. RelativeLayout

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Do you like here?" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="Maybe i do" />

    <Button
        android:id="@+id/yeah_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Yeah" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/yeah_button"
        android:layout_toLeftOf="@id/yeah_button"
        android:text="Cat" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/yeah_button"
        android:layout_toRightOf="@id/yeah_button"
        android:text="Fish" />

</RelativeLayout>

3. FrameLayout

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LoveLike" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="NicoNicoNI" />

</FrameLayout>

4. PercentFrameLayout

<android.support.percent.PercentFrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <Button
        android:id="@+id/button_b"
        android:layout_gravity="left"
        android:text="First"
        app:layout_heightPercent="10%"
        app:layout_widthPercent="30%" />


    <Button
        android:id="@+id/button_c"
        android:layout_gravity="center"
        android:text="Second"
        app:layout_heightPercent="10%"
        app:layout_widthPercent="30%" />


    <Button
        android:id="@+id/button_d"
        android:layout_gravity="right"
        android:text="Third"
        app:layout_heightPercent="10%"
        app:layout_widthPercent="30%" />

</android.support.percent.PercentFrameLayout>

这几个玩意其实还是挺简单的,不过感觉不写下来感觉容易忘,可能是老了,= _ =
附上测试的效果图,能看出来里面有多少元素么。

<style>.imgCon img{width:320px}</style>
<div class="imgCon">

S70427-203132.jpg

</div>

粤公网安备 44011102000950号