SJ127 篮球积分器
這周一次來了兩個課程,很激動啊,其實是累得tu啊。
主題 court counter
一. 目標
目標 籃球計分器
- 類似雙欄的布局(兩路linearl layout)(layout嵌套)
- 學習view style屬性:gravity
- 界面優化技巧
- 一種功能對應一種顏色
- 溫習變量作用域
- 全局變量記錄比分
- 涉及籃球比賽規則
- 重置比分
二. 本輪 new concept
問題引出
- 除了整個布局的四個角(詳細是buttom/top和left/right組合),還能不能在一個子視圖里面移動(居中,靠左,靠右)。。。
solution
layout_gravity: 定位一個子視圖應該在父視圖的哪一塊。
https://developer.android.com/intl/zh-cn/reference/android/widget/LinearLayout.LayoutParams.html#attr_android:layout_gravity
gravity: 定位內容應在對象的哪一塊。
https://developer.android.com/intl/zh-cn/reference/android/widget/LinearLayout.html#attr_android:gravity
三 界面設計截圖
待
代碼過程
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
- 先寫Java的方法,后寫XML,可以自動提示
- 第一次見到兩個豎直并列的linear layout
- 升級AS后,styles.xml要注意以下細節往Theme之前加Base(如果Theme沒有),不然會報The following classes could not be instantiated
android.support.v7.internal.widget.ActionBarOverlayLayout
五.后話
這是第一次在真機note 2上錄制,
首先錄制后是橫屏的,
其次adb shell screencapture 在我的android 4.3上命令不能用
android studio也不支持錄制。
一共耗了3個小時找了各種工具搞定,要有一個好的demo不容易啊!
代碼時間一半
錄屏時間大半。。。
六.參考
bug: The following classes could not be instantiated
mp4轉gif
感謝 昵圖網圖標
旋轉 屏幕 MP4
git on udacity
more filp and mirror
總結
以上是生活随笔為你收集整理的SJ127 篮球积分器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 抖音账号和视频都没有问题,为什么我的流量
- 下一篇: rsa的简单学习