SJ127 篮球积分器
這周一次來(lái)了兩個(gè)課程,很激動(dòng)啊,其實(shí)是累得tu啊。
主題 court counter
一. 目標(biāo)
目標(biāo) 籃球計(jì)分器
- 類似雙欄的布局(兩路linearl layout)(layout嵌套)
- 學(xué)習(xí)view style屬性:gravity
- 界面優(yōu)化技巧
- 一種功能對(duì)應(yīng)一種顏色
- 溫習(xí)變量作用域
- 全局變量記錄比分
- 涉及籃球比賽規(guī)則
- 重置比分
二. 本輪 new concept
問(wèn)題引出
- 除了整個(gè)布局的四個(gè)角(詳細(xì)是buttom/top和left/right組合),還能不能在一個(gè)子視圖里面移動(dòng)(居中,靠左,靠右)。。。
solution
layout_gravity: 定位一個(gè)子視圖應(yīng)該在父視圖的哪一塊。
https://developer.android.com/intl/zh-cn/reference/android/widget/LinearLayout.LayoutParams.html#attr_android:layout_gravity
gravity: 定位內(nèi)容應(yīng)在對(duì)象的哪一塊。
https://developer.android.com/intl/zh-cn/reference/android/widget/LinearLayout.html#attr_android:gravity
三 界面設(shè)計(jì)截圖
待
代碼過(guò)程
java代碼
package com.example.android.courtcounter;import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.TextView;/*** This activity keeps track of the basketball score for 2 teams.*/ public class MainActivity extends AppCompatActivity {// Tracks the score for Team Aint scoreTeamA = 0;// Tracks the score for Team Bint scoreTeamB = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);String jamesbond = "hi";String jamesBond = "hello";String s = jamesBond + jamesbond;}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.menu_main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();//noinspection SimpSlifiableIfStatementif (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}/*** Increase the score for Team A by 1 point.*/public void addOneForTeamA(View v) {scoreTeamA = scoreTeamA + 1;displayForTeamA(scoreTeamA);}/*** Increase the score for Team A by 2 points.*/public void addTwoForTeamA(View v) {scoreTeamA = scoreTeamA + 2;displayForTeamA(scoreTeamA);}/*** Increase the score for Team A by 3 points.*/public void addThreeForTeamA(View v) {scoreTeamA = scoreTeamA + 3;displayForTeamA(scoreTeamA);}/*** Increase the score for Team B by 1 point.*/public void addOneForTeamB(View v) {scoreTeamB = scoreTeamB + 1;displayForTeamB(scoreTeamB);}/*** Increase the score for Team B by 2 points.*/public void addTwoForTeamB(View v) {scoreTeamB = scoreTeamB + 2;displayForTeamB(scoreTeamB);}/*** Increase the score for Team B by 3 points.*/public void addThreeForTeamB(View v) {scoreTeamB = scoreTeamB + 3;displayForTeamB(scoreTeamB);}/*** Resets the score for both teams back to 0.*/public void resetScore(View v) {scoreTeamA = 0;scoreTeamB = 0;displayForTeamA(scoreTeamA);displayForTeamB(scoreTeamB);}/*** Displays the given score for Team A.*/public void displayForTeamA(int score) {TextView scoreView = (TextView) findViewById(R.id.team_a_score);scoreView.setText(String.valueOf(score));}/*** Displays the given score for Team B.*/public void displayForTeamB(int score) {TextView scoreView = (TextView) findViewById(R.id.team_b_score);scoreView.setText(String.valueOf(score));} }layout
再額外的布局上,我加上了圖片,自己添加了drawable文件夾
<?xml version="1.0" encoding="utf-8"?><!-- Layout for the basketball score counter. --> <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"><LinearLayout android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><LinearLayout android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:orientation="vertical"><ImageView android:layout_width="120dp"android:layout_height="120dp"android:gravity="center"android:src="@drawable/i1"android:layout_gravity="center_horizontal" /><TextView android:layout_width="match_parent"android:layout_height="wrap_content"android:fontFamily="sans-serif-medium"android:gravity="center"android:padding="16dp"android:text="Team A"android:textColor="#616161"android:textSize="14sp" /><TextView android:id="@+id/team_a_score"android:layout_width="match_parent"android:layout_height="wrap_content"android:fontFamily="sans-serif-light"android:gravity="center"android:paddingBottom="24dp"android:text="0"android:textColor="#000000"android:textSize="56sp" /><Button android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="8dp"android:layout_marginLeft="24dp"android:layout_marginRight="24dp"android:onClick="addThreeForTeamA"android:text="+3 Points" /><Button android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="8dp"android:layout_marginLeft="24dp"android:layout_marginRight="24dp"android:onClick="addTwoForTeamA"android:text="+2 Points" /><Button android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="8dp"android:layout_marginLeft="24dp"android:layout_marginRight="24dp"android:onClick="addOneForTeamA"android:text="Free throw" /></LinearLayout><View android:layout_width="1dp"android:layout_height="match_parent"android:layout_marginTop="16dp"android:background="@android:color/darker_gray" /><LinearLayout android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:orientation="vertical"><ImageView android:layout_width="120dp"android:layout_height="120dp"android:gravity="center"android:src="@drawable/i3"android:layout_gravity="center_horizontal" /><TextView android:layout_width="match_parent"android:layout_height="wrap_content"android:fontFamily="sans-serif-medium"android:gravity="center"android:padding="16dp"android:text="Team B"android:textColor="#616161"android:textSize="14sp" /><TextView android:id="@+id/team_b_score"android:layout_width="match_parent"android:layout_height="wrap_content"android:fontFamily="sans-serif-light"android:gravity="center"android:paddingBottom="24dp"android:text="0"android:textColor="#000000"android:textSize="56sp" /><Button android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="8dp"android:layout_marginLeft="24dp"android:layout_marginRight="24dp"android:onClick="addThreeForTeamB"android:text="+3 Points" /><Button android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="8dp"android:layout_marginLeft="22dp"android:layout_marginRight="22dp"android:onClick="addTwoForTeamB"android:text="+2 Points" /><Button android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="8dp"android:layout_marginLeft="22dp"android:layout_marginRight="22dp"android:onClick="addOneForTeamB"android:text="Free throw" /></LinearLayout></LinearLayout><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:layout_marginBottom="32dp"android:onClick="resetScore"android:text="Reset" /> </RelativeLayout>四. tips
- 先寫(xiě)Java的方法,后寫(xiě)XML,可以自動(dòng)提示
- 第一次見(jiàn)到兩個(gè)豎直并列的linear layout
- 升級(jí)AS后,styles.xml要注意以下細(xì)節(jié)往Theme之前加Base(如果Theme沒(méi)有),不然會(huì)報(bào)The following classes could not be instantiated
android.support.v7.internal.widget.ActionBarOverlayLayout
五.后話
這是第一次在真機(jī)note 2上錄制,
首先錄制后是橫屏的,
其次adb shell screencapture 在我的android 4.3上命令不能用
android studio也不支持錄制。
一共耗了3個(gè)小時(shí)找了各種工具搞定,要有一個(gè)好的demo不容易啊!
代碼時(shí)間一半
錄屏?xí)r間大半。。。
六.參考
bug: The following classes could not be instantiated
mp4轉(zhuǎn)gif
感謝 昵圖網(wǎng)圖標(biāo)
旋轉(zhuǎn) 屏幕 MP4
git on udacity
more filp and mirror
總結(jié)
以上是生活随笔為你收集整理的SJ127 篮球积分器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 抖音账号和视频都没有问题,为什么我的流量
- 下一篇: rsa的简单学习