Android初学者,首次在CSDN上写文章,如有任何不足与错误,请多多指教,希望能得到有经验的前辈们的指点,万分感谢
Google安卓开发者官方视频培训课程入门之计分器App
官方课程上只是给出了一个UI界面的设计步骤,但是怎么把这个App完整的做出来却并没有说明,煞费一番苦心的在google里搜索教程,结果也不如人意,于是萌生了在自己做完之后写一篇记录的想法,好了废话不多说,直接切入正题:
UI界面的构思:
1. 将主屏分成两个容器,布局格式为LinearLayout,orientation设置为vertical,这样就构造好了容器1与容器2,并且按照比例布局的方式设置为3:1;容器1用来放置TextView和各自的三个Button,容器2用来放置reset的Button。
2. 容器1:将容器1中放入三个LinearLayout布局,orientation设置为horizontal,这里暂且称之为A,C,B,A与B用来放置两支team相关的控件,C用来设置中间的分割线。
3. A容器(orientation设置为vertical)中放置2个TextView,用来显示队名以及分数,之后再在其中放入一个LinearLayout布局a(目的是为了保证三个分数Button能够更好的排版,布局的属性设置为width>>>match_parent,height>>>match_parent, orientation依旧设置为vertical),在a中放入三个Button,按比例平均分配在a容器的空间,设置好各自margin_left/right/bottom/top的值,优化界面美感;B容器中同样的操作。
4. 最后是分割线与reset的设置。关于分割线,height设置为match_parent,background属性值为颜色深色。reset直接在2容器中添加就可以,2容器layout_gravity与gravity设置为center,添加Button即可
5. 最后可以选择性的设置Button字体,颜色,TextView字体,颜色,还有标题栏以及通知栏的颜色(在valus文件夹的color里面有这两个的颜色,直接修改colorPrimery与colorPrimeryDark即可)
UI界面的效果图:
布局源代码:
<?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="com.example.aaron_gan.baskteballpints.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="horizontal"
>
//team A的布局
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:gravity="center"
android:text="@string/teamA"
android:textSize="15sp" />
<TextView
android:id="@+id/points_teamA"
android:layout_width="match_parent"
android:layout_height="150dp"
android:gravity="center"
android:text="0"
android:textColor="#000000"
android:textSize="100sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/teamA_3"
android:layout_marginTop="3dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="6dp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="@string/three_points"
android:textSize="20sp"
android:background="#FF6F00"
android:textColor="#000000"/>
<Button
android:id="@+id/teamA_2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="6dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:layout_weight="1"
android:background="#FF6F00"
android:text="@string/two_points"
android:textColor="#000000"
android:textSize="20sp" />
<Button
android:id="@+id/teamA_free"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:layout_weight="1"
android:background="#FF6F00"
android:text="@string/free_throw"
android:textColor="#000000"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
//中间的分割线
<LinearLayout
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#9e9e9e">
</LinearLayout>
//team B的布局
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:textSize="15sp"
android:text="@string/teamB"/>
<TextView
android:id="@+id/points_teamB"
android:layout_width="match_parent"
android:layout_height="150dp"
android:text="0"
android:textColor="#000000"
android:textSize="100sp"
android:gravity="center"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/teamB_3"
android:layout_marginTop="3dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="6dp"
android:layout_marginBottom="6dp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="@string/three_points"
android:textSize="20sp"
android:background="#FF6F00"
android:textColor="#000000"/>
<Button
android:id="@+id/teamB_2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="6dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="6dp"
android:layout_marginTop="3dp"
android:layout_weight="1"
android:background="#FF6F00"
android:text="@string/two_points"
android:textColor="#000000"
android:textSize="20sp" />
<Button
android:id="@+id/teamB_free"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="6dp"
android:layout_marginTop="3dp"
android:layout_weight="1"
android:background="#FF6F00"
android:text="@string/free_throw"
android:textColor="#000000"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center"
android:layout_gravity="center"
android:layout_weight="1">
//这里定义一个按钮控价显示在下半个区域的正中间,名字是Reset
<Button
android:id="@+id/btn_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FF6F00"
android:text="@string/btn_reset"
android:textAllCaps="false"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
MainActivity.java中的源代码:
package com.example.aaron_gan.baskteballpints;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//两队的分数
private int pointsA = 0;
private int pointsB = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//七个按钮的声明
Button reset = (Button) findViewById(R.id.btn_reset);
Button a3 = (Button) findViewById(R.id.teamA_3);
Button a2 = (Button) findViewById(R.id.teamA_2);
Button afree = (Button) findViewById(R.id.teamA_free);
Button b3 = (Button) findViewById(R.id.teamB_3);
Button b2 = (Button) findViewById(R.id.teamB_2);
Button bfree = (Button) findViewById(R.id.teamB_free);
final TextView points_teamA = (TextView) findViewById(R.id.points_teamA);
final TextView points_teamB = (TextView) findViewById(R.id.points_teamB);
//reset
reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pointsA = 0;
pointsB = 0;
points_teamA.setText(String.valueOf(pointsA));
points_teamB.setText(String.valueOf(pointsB));
}
});
//team A 的三分
a3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pointsA = pointsA+3;
points_teamA.setText(String.valueOf(pointsA));
}
});
//team A 的两分
a2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pointsA = pointsA+2;
points_teamA.setText(String.valueOf(pointsA));
}
});
//team A 的罚球
afree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pointsA = pointsA+1;
points_teamA.setText(String.valueOf(pointsA));
}
});
//team B 的三分
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pointsB = pointsB+3;
points_teamB.setText(String.valueOf(pointsB));
}
});
//team B 的两分
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pointsB = pointsB+2;
points_teamB.setText(String.valueOf(pointsB));
}
});
//team B 的罚球
bfree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pointsB = pointsB+1;
points_teamB.setText(String.valueOf(pointsB));
}
});
}
}
*****这里有一个值得注意的方法是String.valueOf(),目的就是为了把算数运算得到的分数转化为String类型顺利的通过setText方法输出
*****最初在百度上搜到过一个方法直接可以在运算的同时将运算的数字输入进TextView中,但貌似并不能用,所以如果有幸能够被知道的大神看到这个问题,万分感谢您的不吝赐教
*****最后再贴一个完成品的链接吧,有兴趣的同伴可以下载下来看一看,谢谢Basketball_Points百度云链接