实践是最好的学习方式,技术也如此。
Button1
和 Button2
)和一个 TextView
;Button1
,屏幕显示一条消息(a Toast);点击 Button2
增加 TextView
中显示的 “计数器” ,计数器从 0
开始;View
View
和 ViewGroup
对象的层次结构进行构建;View
通常用于绘制用户可见的并与之交互的内容;ViewGroup
是不可见的容器,用于定义 View 和其它 ViewGroup 对象的布局结构;
Button
或TextView
;LinearLayout
或 ConstraintLayout
;常用属性
match_parent
layout_width
或 layout_height
;Wrap_content
(指占满父容器此时要控件的宽或高等于父容器的宽或高);
layout_width
或 layout_height
;调色板窗格:显示
组件树窗格:显示 UI 元素的视图层次结构;View 元素被组织成父级和子级的树形层次结构,子级继承其父级的属性;
创建布局
为 Button 添加 OnClick 属性和处理程序;单击处理程序是当用户单击或点击可单击 UI 元素时调用的方法
public class MainActivity extends AppCompatActivity {
private int mCount = 0;
private TextView mShowCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // 指定一个视图
Log.i("myapplication", "1521");
}
public void showToast(View view) {
Toast toast = Toast.makeText(this, R.string.toast_message, Toast.LENGTH_SHORT);
toast.show();
}
public void countUp(View view) {
mCount ++;
mShowCount = (TextView) findViewById(R.id.show_count);
if (mShowCount != null) {
mShowCount.setText(Integer.toString(mCount));
}
}
}
<?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">
<Button
android:id="@+id/button_toast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@android:color/holo_purple"
android:text="@string/button_label_toast"
android:textColor="@android:color/black"
android:onClick="showToast" />
<TextView
android:id="@+id/show_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFF00"
android:gravity="center"
android:text="@string/count_initial_value"
android:textColor="@android:color/holo_purple"
android:textSize="160sp"
android:textStyle="bold" />
<Button
android:id="@+id/button_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@android:color/holo_purple"
android:text="@string/button_label_count"
android:textColor="@android:color/black"
android:onClick="countUp" />
</LinearLayout>
android:layout_below="@+id/xxx"
;android:layout_centerHorizontal="true"
android:layout_below=“@+id/show_count”:相对于其他视图的位置
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<Button
android:id="@+id/button_toast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@android:color/holo_purple"
android:text="@string/button_label_toast"
android:textColor="@android:color/black"
android:onClick="showToast" />
<TextView
android:id="@+id/show_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFF00"
android:gravity="center"
android:text="@string/count_initial_value"
android:textColor="@android:color/holo_purple"
android:textSize="160sp"
android:textStyle="bold"
android:layout_below="@+id/button_toast"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>
<Button
android:id="@+id/button_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@android:color/holo_purple"
android:text="@string/button_label_count"
android:textColor="@android:color/black"
android:onClick="countUp"
android:layout_below="@+id/show_count"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.scrollingtext.MainActivity">
<TextView
android:id="@+id/article_heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:padding="@dimen/padding_regular"
android:text="@string/article_title"
android:textAppearance=
"@android:style/TextAppearance.DeviceDefault.Large"
android:textColor="@android:color/white"
android:textStyle="bold" />
<TextView
android:id="@+id/article_subheading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/article_heading"
android:padding="@dimen/padding_regular"
android:text="@string/article_subtitle"
android:textAppearance=
"@android:style/TextAppearance.DeviceDefault" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/article_subheading">
<TextView
android:id="@+id/article"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:lineSpacingExtra="@dimen/line_spacing"
android:padding="@dimen/padding_regular"
android:text="@string/article_text" />
</ScrollView>
</RelativeLayout>
ViewGroup
(例如 LinearLayout )作为 ScrollView 中的子 View 来滚动多个 View 元素。将元素括在 LinearLayout 内<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/article_heading"
android:background="@color/head_backgroud"
android:textColor="@android:color/white"
android:padding="@dimen/padding_regular"
android:textAppearance="@android:style/TextAppearance.DeviceDefault"
android:textStyle="bold"
android:text="@string/article_title"/>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/article_heading">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/article_subheading"
android:padding="@dimen/padding_regular"
android:textAppearance="@android:style/TextAppearance.DeviceDefault"
android:text="@string/article_subtitle"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/article"
android:autoLink="web"
android:padding="@dimen/padding_regular"
android:text="@string/article_text"
android:lineSpacingExtra="@dimen/line_spacing"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>