制作 Android Studio 中的 3×3 按钮

huangapple go评论150阅读模式
英文:

Making 3 by 3 buttons in Android Studio

问题

以下是翻译好的内容:

我是Android Studio的新手。我试图按照YouTube视频上的说明来创建一个井字棋游戏,但其中一部分指令不太清楚。

目前我有这个:

(第一个图片)

但我想要的是这个:

(第二个图片)

我的 activity_main.xml 文件代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context=".MainActivity">
  9. (布局部分,与您提供的内容一致)
  10. </LinearLayout>

我的 MainActivity.java 文件代码如下:

  1. package com.example.tictactoe;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.widget.Button;
  5. import android.widget.TextView;
  6. public class MainActivity extends AppCompatActivity {
  7. 变量和方法部分与您提供的内容一致
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12. }
  13. }

我感觉主要问题可能出在 activity_main.xml 文件中的按钮部分。

我在代码的哪个部分做错了?

英文:

I am new to learning how to use Android Studio. I was trying to follow a Youtube Video on how to create a game of TicTacToe and some part of the instruction was unclear.

At the moment, I have this:

制作 Android Studio 中的 3×3 按钮

But I want to be like this:

制作 Android Studio 中的 3×3 按钮

My activity_main.xml file code:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context=".MainActivity">
  9. <RelativeLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content">
  12. <TextView
  13. android:id="@+id/text_view_p1"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="Player 1 : 0"
  17. android:textSize="30sp" />
  18. <TextView
  19. android:id="@+id/text_view_p2"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_below="@+id/text_view_p1"
  23. android:text="Player 2 : 0"
  24. android:textSize="30sp" />
  25. <Button
  26. android:id="@+id/button_reset"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:layout_alignParentEnd="true"
  30. android:layout_centerVertical="true"
  31. android:layout_marginEnd="41dp"
  32. android:text="reset" />
  33. </RelativeLayout>
  34. <LinearLayout
  35. android:layout_width="match_parent"
  36. android:layout_height="0dp"
  37. android:layout_weight="1">
  38. <Button
  39. android:id="@+id/button_00"
  40. android:layout_width="0dp"
  41. android:layout_height="match_parent"
  42. android:layout_weight="1"
  43. android:textSize="60sp" />
  44. <Button
  45. android:id="@+id/button_01"
  46. android:layout_width="0dp"
  47. android:layout_height="match_parent"
  48. android:layout_weight="1"
  49. android:textSize="60sp" />
  50. <Button
  51. android:id="@+id/button_02"
  52. android:layout_width="0dp"
  53. android:layout_height="match_parent"
  54. android:layout_weight="1"
  55. android:textSize="60sp" />
  56. <Button
  57. android:id="@+id/button_10"
  58. android:layout_width="0dp"
  59. android:layout_height="match_parent"
  60. android:layout_weight="1"
  61. android:textSize="60sp" />
  62. <Button
  63. android:id="@+id/button_11"
  64. android:layout_width="0dp"
  65. android:layout_height="match_parent"
  66. android:layout_weight="1"
  67. android:textSize="60sp" />
  68. <Button
  69. android:id="@+id/button_12"
  70. android:layout_width="0dp"
  71. android:layout_height="match_parent"
  72. android:layout_weight="1"
  73. android:textSize="60sp" />
  74. </LinearLayout>
  75. </LinearLayout>

My MainActivity.java file code:

  1. package com.example.tictactoe;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.widget.Button;
  5. import android.widget.TextView;
  6. public class MainActivity extends AppCompatActivity {
  7. private Button[][] buttons = new Button[3][3];
  8. private boolean player1Turn = true;
  9. private int rountCount;
  10. private int player1Points;
  11. private int player2Points;
  12. private TextView textViewplayer1;
  13. private TextView textViewplayer2;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. }
  19. }

I feel that mostly something is not correct with the button section of the script of the activity_main.xml file.

Which part of my code am I not doing correctly?

答案1

得分: 2

以下是您要翻译的内容:

你的做法是错误的,首先不要将父布局的高度设置为“match parent”,其次你需要使用嵌套的“线性布局”来实现你的目标,我建议你改用“约束布局”。

我已经为你留下了最后一行要完成的部分,并且我已经整理好了你的代码,所以我确信在完成时你不会遇到任何问题 制作 Android Studio 中的 3×3 按钮

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="wrap_content"
  7. android:orientation="vertical"
  8. tools:context=".MainActivity">
  9. <RelativeLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content">
  12. <TextView
  13. android:id="@+id/text_view_p1"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="Player 1 : 0"
  17. android:textSize="30sp" />
  18. <TextView
  19. android:id="@+id/text_view_p2"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_below="@+id/text_view_p1"
  23. android:text="Player 2 : 0"
  24. android:textSize="30sp" />
  25. <Button
  26. android:id="@+id/button_reset"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:layout_alignParentEnd="true"
  30. android:layout_centerVertical="true"
  31. android:layout_marginEnd="41dp"
  32. android:text="reset" />
  33. </RelativeLayout>
  34. <LinearLayout
  35. android:layout_width="match_parent"
  36. android:layout_height="0dp"
  37. android:layout_weight="1"
  38. android:orientation="vertical">
  39. <LinearLayout
  40. android:layout_width="match_parent"
  41. android:layout_height="wrap_content"
  42. android:orientation="horizontal">
  43. <Button
  44. android:id="@+id/button_00"
  45. android:layout_width="wrap_content"
  46. android:layout_height="match_parent"
  47. android:layout_weight="1"
  48. android:textSize="60sp" />
  49. <Button
  50. android:id="@+id/button_01"
  51. android:layout_width="wrap_content"
  52. android:layout_height="match_parent"
  53. android:layout_weight="1"
  54. android:textSize="60sp" />
  55. <Button
  56. android:id="@+id/button_02"
  57. android:layout_width="wrap_content"
  58. android:layout_height="match_parent"
  59. android:layout_weight="1"
  60. android:textSize="60sp" />
  61. </LinearLayout>
  62. <LinearLayout
  63. android:layout_width="match_parent"
  64. android:layout_height="wrap_content"
  65. android:orientation="horizontal">
  66. <Button
  67. android:id="@+id/button_10"
  68. android:layout_width="0dp"
  69. android:layout_height="match_parent"
  70. android:layout_weight="1"
  71. android:textSize="60sp" />
  72. <Button
  73. android:id="@+id/button_11"
  74. android:layout_width="0dp"
  75. android:layout_height="match_parent"
  76. android:layout_weight="1"
  77. android:textSize="60sp" />
  78. <Button
  79. android:id="@+id/button_12"
  80. android:layout_width="0dp"
  81. android:layout_height="match_parent"
  82. android:layout_weight="1"
  83. android:textSize="60sp" />
  84. </LinearLayout>
  85. </LinearLayout>
  86. </LinearLayout>
英文:

You are doing it wrong first don't make height of your parent layout to match parent and secondly you have to user nested linear layouts to achieve your goal I would suggest you to use constraint layout instead.

I have left the last row for you to complete, and I have sorted your code out so I am sure you won't find any issue doing that 制作 Android Studio 中的 3×3 按钮

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:layout_width=&quot;match_parent&quot;
  6. android:layout_height=&quot;wrap_content&quot;
  7. android:orientation=&quot;vertical&quot;
  8. tools:context=&quot;.MainActivity&quot;&gt;
  9. &lt;RelativeLayout
  10. android:layout_width=&quot;match_parent&quot;
  11. android:layout_height=&quot;wrap_content&quot;&gt;
  12. &lt;TextView
  13. android:id=&quot;@+id/text_view_p1&quot;
  14. android:layout_width=&quot;wrap_content&quot;
  15. android:layout_height=&quot;wrap_content&quot;
  16. android:text=&quot;Player 1 : 0&quot;
  17. android:textSize=&quot;30sp&quot; /&gt;
  18. &lt;TextView
  19. android:id=&quot;@+id/text_view_p2&quot;
  20. android:layout_width=&quot;wrap_content&quot;
  21. android:layout_height=&quot;wrap_content&quot;
  22. android:layout_below=&quot;@+id/text_view_p1&quot;
  23. android:text=&quot;Player 2 : 0&quot;
  24. android:textSize=&quot;30sp&quot; /&gt;
  25. &lt;Button
  26. android:id=&quot;@+id/button_reset&quot;
  27. android:layout_width=&quot;wrap_content&quot;
  28. android:layout_height=&quot;wrap_content&quot;
  29. android:layout_alignParentEnd=&quot;true&quot;
  30. android:layout_centerVertical=&quot;true&quot;
  31. android:layout_marginEnd=&quot;41dp&quot;
  32. android:text=&quot;reset&quot; /&gt;
  33. &lt;/RelativeLayout&gt;
  34. &lt;LinearLayout
  35. android:layout_width=&quot;match_parent&quot;
  36. android:layout_height=&quot;0dp&quot;
  37. android:layout_weight=&quot;1&quot;
  38. android:orientation=&quot;vertical&quot;&gt;
  39. &lt;LinearLayout
  40. android:layout_width=&quot;match_parent&quot;
  41. android:layout_height=&quot;wrap_content&quot;
  42. android:orientation=&quot;horizontal&quot;&gt;
  43. &lt;Button
  44. android:id=&quot;@+id/button_00&quot;
  45. android:layout_width=&quot;wrap_content&quot;
  46. android:layout_height=&quot;match_parent&quot;
  47. android:layout_weight=&quot;1&quot;
  48. android:textSize=&quot;60sp&quot; /&gt;
  49. &lt;Button
  50. android:id=&quot;@+id/button_01&quot;
  51. android:layout_width=&quot;wrap_content&quot;
  52. android:layout_height=&quot;match_parent&quot;
  53. android:layout_weight=&quot;1&quot;
  54. android:textSize=&quot;60sp&quot; /&gt;
  55. &lt;Button
  56. android:id=&quot;@+id/button_02&quot;
  57. android:layout_width=&quot;wrap_content&quot;
  58. android:layout_height=&quot;match_parent&quot;
  59. android:layout_weight=&quot;1&quot;
  60. android:textSize=&quot;60sp&quot; /&gt;
  61. &lt;/LinearLayout&gt;
  62. &lt;LinearLayout
  63. android:layout_width=&quot;match_parent&quot;
  64. android:layout_height=&quot;wrap_content&quot;
  65. android:orientation=&quot;horizontal&quot;&gt;
  66. &lt;Button
  67. android:id=&quot;@+id/button_10&quot;
  68. android:layout_width=&quot;0dp&quot;
  69. android:layout_height=&quot;match_parent&quot;
  70. android:layout_weight=&quot;1&quot;
  71. android:textSize=&quot;60sp&quot; /&gt;
  72. &lt;Button
  73. android:id=&quot;@+id/button_11&quot;
  74. android:layout_width=&quot;0dp&quot;
  75. android:layout_height=&quot;match_parent&quot;
  76. android:layout_weight=&quot;1&quot;
  77. android:textSize=&quot;60sp&quot; /&gt;
  78. &lt;Button
  79. android:id=&quot;@+id/button_12&quot;
  80. android:layout_width=&quot;0dp&quot;
  81. android:layout_height=&quot;match_parent&quot;
  82. android:layout_weight=&quot;1&quot;
  83. android:textSize=&quot;60sp&quot; /&gt;
  84. &lt;/LinearLayout&gt;
  85. &lt;/LinearLayout&gt;
  86. &lt;/LinearLayout&gt;

制作 Android Studio 中的 3×3 按钮

答案2

得分: 0

  1. 将按钮的高度设置为wrap_content而不是match_parent
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:orientation="vertical"
  9. tools:context=".MainActivity">
  10. <RelativeLayout
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content">
  13. <TextView
  14. android:id="@+id/text_view_p1"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="Player 1 : 0"
  18. android:textSize="30sp" />
  19. <TextView
  20. android:id="@+id/text_view_p2"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_below="@+id/text_view_p1"
  24. android:text="Player 2 : 0"
  25. android:textSize="30sp" />
  26. <Button
  27. android:id="@+id/button_reset"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:layout_alignParentEnd="true"
  31. android:layout_centerVertical="true"
  32. android:layout_marginEnd="41dp"
  33. android:text="reset" />
  34. </RelativeLayout>
  35. <LinearLayout
  36. android:layout_width="match_parent"
  37. android:layout_height="0dp"
  38. android:layout_weight="1">
  39. <Button
  40. android:id="@+id/button_00"
  41. android:layout_width="0dp"
  42. android:layout_height="wrap_content"
  43. android:layout_weight="1"
  44. android:textSize="60sp" />
  45. <Button
  46. android:id="@+id/button_01"
  47. android:layout_width="0dp"
  48. android:layout_height="wrap_content"
  49. android:layout_weight="1"
  50. android:textSize="60sp" />
  51. <Button
  52. android:id="@+id/button_02"
  53. android:layout_width="0dp"
  54. android:layout_height="wrap_content"
  55. android:layout_weight="1"
  56. android:textSize="60sp" />
  57. <Button
  58. android:id="@+id/button_10"
  59. android:layout_width="0dp"
  60. android:layout_height="wrap_content"
  61. android:layout_weight="1"
  62. android:textSize="60sp" />
  63. <Button
  64. android:id="@+id/button_11"
  65. android:layout_width="0dp"
  66. android:layout_height="wrap_content"
  67. android:layout_weight="1"
  68. android:textSize="60sp" />
  69. <Button
  70. android:id="@+id/button_12"
  71. android:layout_width="0dp"
  72. android:layout_height="wrap_content"
  73. android:layout_weight="1"
  74. android:textSize="60sp" />
  75. </LinearLayout>
  76. </LinearLayout>
英文:

Set heights of buttons to wrap_content instead of match_parent:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:layout_width=&quot;match_parent&quot;
  6. android:layout_height=&quot;match_parent&quot;
  7. android:orientation=&quot;vertical&quot;
  8. tools:context=&quot;.MainActivity&quot;&gt;
  9. &lt;RelativeLayout
  10. android:layout_width=&quot;match_parent&quot;
  11. android:layout_height=&quot;wrap_content&quot;&gt;
  12. &lt;TextView
  13. android:id=&quot;@+id/text_view_p1&quot;
  14. android:layout_width=&quot;wrap_content&quot;
  15. android:layout_height=&quot;wrap_content&quot;
  16. android:text=&quot;Player 1 : 0&quot;
  17. android:textSize=&quot;30sp&quot; /&gt;
  18. &lt;TextView
  19. android:id=&quot;@+id/text_view_p2&quot;
  20. android:layout_width=&quot;wrap_content&quot;
  21. android:layout_height=&quot;wrap_content&quot;
  22. android:layout_below=&quot;@+id/text_view_p1&quot;
  23. android:text=&quot;Player 2 : 0&quot;
  24. android:textSize=&quot;30sp&quot; /&gt;
  25. &lt;Button
  26. android:id=&quot;@+id/button_reset&quot;
  27. android:layout_width=&quot;wrap_content&quot;
  28. android:layout_height=&quot;wrap_content&quot;
  29. android:layout_alignParentEnd=&quot;true&quot;
  30. android:layout_centerVertical=&quot;true&quot;
  31. android:layout_marginEnd=&quot;41dp&quot;
  32. android:text=&quot;reset&quot; /&gt;
  33. &lt;/RelativeLayout&gt;
  34. &lt;LinearLayout
  35. android:layout_width=&quot;match_parent&quot;
  36. android:layout_height=&quot;0dp&quot;
  37. android:layout_weight=&quot;1&quot;&gt;
  38. &lt;Button
  39. android:id=&quot;@+id/button_00&quot;
  40. android:layout_width=&quot;0dp&quot;
  41. android:layout_height=&quot;wrap_content&quot;
  42. android:layout_weight=&quot;1&quot;
  43. android:textSize=&quot;60sp&quot; /&gt;
  44. &lt;Button
  45. android:id=&quot;@+id/button_01&quot;
  46. android:layout_width=&quot;0dp&quot;
  47. android:layout_height=&quot;wrap_content&quot;
  48. android:layout_weight=&quot;1&quot;
  49. android:textSize=&quot;60sp&quot; /&gt;
  50. &lt;Button
  51. android:id=&quot;@+id/button_02&quot;
  52. android:layout_width=&quot;0dp&quot;
  53. android:layout_height=&quot;wrap_content&quot;
  54. android:layout_weight=&quot;1&quot;
  55. android:textSize=&quot;60sp&quot; /&gt;
  56. &lt;Button
  57. android:id=&quot;@+id/button_10&quot;
  58. android:layout_width=&quot;0dp&quot;
  59. android:layout_height=&quot;wrap_content&quot;
  60. android:layout_weight=&quot;1&quot;
  61. android:textSize=&quot;60sp&quot; /&gt;
  62. &lt;Button
  63. android:id=&quot;@+id/button_11&quot;
  64. android:layout_width=&quot;0dp&quot;
  65. android:layout_height=&quot;wrap_content&quot;
  66. android:layout_weight=&quot;1&quot;
  67. android:textSize=&quot;60sp&quot; /&gt;
  68. &lt;Button
  69. android:id=&quot;@+id/button_12&quot;
  70. android:layout_width=&quot;0dp&quot;
  71. android:layout_height=&quot;wrap_content&quot;
  72. android:layout_weight=&quot;1&quot;
  73. android:textSize=&quot;60sp&quot; /&gt;
  74. &lt;/LinearLayout&gt;
  75. &lt;/LinearLayout&gt;

答案3

得分: 0

以下是您提供的代码的翻译部分:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context=".MainActivity">
  9. <RelativeLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content">
  12. <TextView
  13. android:id="@+id/text_view_p1"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="Player 1 : 0"
  17. android:textSize="30sp" />
  18. <TextView
  19. android:id="@+id/text_view_p2"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_below="@+id/text_view_p1"
  23. android:text="Player 2 : 0"
  24. android:textSize="30sp" />
  25. <Button
  26. android:id="@+id/button_reset"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:layout_alignParentEnd="true"
  30. android:layout_centerVertical="true"
  31. android:layout_marginEnd="41dp"
  32. android:text="reset" />
  33. </RelativeLayout>
  34. <LinearLayout
  35. android:layout_width="match_parent"
  36. android:layout_height="match_parent"
  37. android:orientation="vertical"
  38. android:weightSum="3" >
  39. <LinearLayout
  40. android:layout_width="match_parent"
  41. android:layout_height="0dp"
  42. android:layout_weight="1"
  43. android:orientation="horizontal"
  44. android:weightSum="3" >
  45. <Button
  46. android:layout_width="0dp"
  47. android:layout_height="fill_parent"
  48. android:layout_weight="1" />
  49. <Button
  50. android:layout_width="0dp"
  51. android:layout_height="fill_parent"
  52. android:layout_weight="1" />
  53. <Button
  54. android:layout_width="0dp"
  55. android:layout_height="fill_parent"
  56. android:layout_weight="1" />
  57. </LinearLayout>
  58. <LinearLayout
  59. android:layout_width="match_parent"
  60. android:layout_height="0dp"
  61. android:layout_weight="1"
  62. android:orientation="horizontal"
  63. android:weightSum="3" >
  64. <Button
  65. android:layout_width="0dp"
  66. android:layout_height="fill_parent"
  67. android:layout_weight="1" />
  68. <Button
  69. android:layout_width="0dp"
  70. android:layout_height="fill_parent"
  71. android:layout_weight="1" />
  72. <Button
  73. android:layout_width="0dp"
  74. android:layout_height="fill_parent"
  75. android:layout_weight="1" />
  76. </LinearLayout>
  77. <LinearLayout
  78. android:layout_width="match_parent"
  79. android:layout_height="0dp"
  80. android:layout_weight="1"
  81. android:orientation="horizontal"
  82. android:weightSum="3" >
  83. <Button
  84. android:layout_width="0dp"
  85. android:layout_height="fill_parent"
  86. android:layout_weight="1" />
  87. <Button
  88. android:layout_width="0dp"
  89. android:layout_height="fill_parent"
  90. android:layout_weight="1" />
  91. <Button
  92. android:layout_width="0dp"
  93. android:layout_height="fill_parent"
  94. android:layout_weight="1" />
  95. </LinearLayout>
  96. </LinearLayout>
  97. </LinearLayout>
英文:

Try this. But not tested.

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:layout_width=&quot;match_parent&quot;
  6. android:layout_height=&quot;match_parent&quot;
  7. android:orientation=&quot;vertical&quot;
  8. tools:context=&quot;.MainActivity&quot;&gt;
  9. &lt;RelativeLayout
  10. android:layout_width=&quot;match_parent&quot;
  11. android:layout_height=&quot;wrap_content&quot;&gt;
  12. &lt;TextView
  13. android:id=&quot;@+id/text_view_p1&quot;
  14. android:layout_width=&quot;wrap_content&quot;
  15. android:layout_height=&quot;wrap_content&quot;
  16. android:text=&quot;Player 1 : 0&quot;
  17. android:textSize=&quot;30sp&quot; /&gt;
  18. &lt;TextView
  19. android:id=&quot;@+id/text_view_p2&quot;
  20. android:layout_width=&quot;wrap_content&quot;
  21. android:layout_height=&quot;wrap_content&quot;
  22. android:layout_below=&quot;@+id/text_view_p1&quot;
  23. android:text=&quot;Player 2 : 0&quot;
  24. android:textSize=&quot;30sp&quot; /&gt;
  25. &lt;Button
  26. android:id=&quot;@+id/button_reset&quot;
  27. android:layout_width=&quot;wrap_content&quot;
  28. android:layout_height=&quot;wrap_content&quot;
  29. android:layout_alignParentEnd=&quot;true&quot;
  30. android:layout_centerVertical=&quot;true&quot;
  31. android:layout_marginEnd=&quot;41dp&quot;
  32. android:text=&quot;reset&quot; /&gt;
  33. &lt;/RelativeLayout&gt;
  34. &lt;LinearLayout
  35. android:layout_width=&quot;match_parent&quot;
  36. android:layout_height=&quot;match_parent&quot;
  37. android:orientation=&quot;vertical&quot;
  38. android:weightSum=&quot;3&quot; &gt;
  39. &lt;LinearLayout
  40. android:layout_width=&quot;match_parent&quot;
  41. android:layout_height=&quot;0dp&quot;
  42. android:layout_weight=&quot;1&quot;
  43. android:orientation=&quot;horizontal&quot;
  44. android:weightSum=&quot;3&quot; &gt;
  45. &lt;Button
  46. android:layout_width=&quot;0dp&quot;
  47. android:layout_height=&quot;fill_parent&quot;
  48. android:layout_weight=&quot;1&quot;
  49. /&gt;
  50. &lt;Button
  51. android:layout_width=&quot;0dp&quot;
  52. android:layout_height=&quot;fill_parent&quot;
  53. android:layout_weight=&quot;1&quot;
  54. /&gt;
  55. &lt;Button
  56. android:layout_width=&quot;0dp&quot;
  57. android:layout_height=&quot;fill_parent&quot;
  58. android:layout_weight=&quot;1&quot;
  59. /&gt;
  60. &lt;/LinearLayout&gt;
  61. &lt;LinearLayout
  62. android:layout_width=&quot;match_parent&quot;
  63. android:layout_height=&quot;0dp&quot;
  64. android:layout_weight=&quot;1&quot;
  65. android:orientation=&quot;horizontal&quot;
  66. android:weightSum=&quot;3&quot; &gt;
  67. &lt;Button
  68. android:layout_width=&quot;0dp&quot;
  69. android:layout_height=&quot;fill_parent&quot;
  70. android:layout_weight=&quot;1&quot;
  71. /&gt;
  72. &lt;Button
  73. android:layout_width=&quot;0dp&quot;
  74. android:layout_height=&quot;fill_parent&quot;
  75. android:layout_weight=&quot;1&quot;
  76. /&gt;
  77. &lt;Button
  78. android:layout_width=&quot;0dp&quot;
  79. android:layout_height=&quot;fill_parent&quot;
  80. android:layout_weight=&quot;1&quot;
  81. /&gt;
  82. &lt;/LinearLayout&gt;
  83. &lt;LinearLayout
  84. android:layout_width=&quot;match_parent&quot;
  85. android:layout_height=&quot;0dp&quot;
  86. android:layout_weight=&quot;1&quot;
  87. android:orientation=&quot;horizontal&quot;
  88. android:weightSum=&quot;3&quot; &gt;
  89. &lt;Button
  90. android:layout_width=&quot;0dp&quot;
  91. android:layout_height=&quot;fill_parent&quot;
  92. android:layout_weight=&quot;1&quot;
  93. /&gt;
  94. &lt;Button
  95. android:layout_width=&quot;0dp&quot;
  96. android:layout_height=&quot;fill_parent&quot;
  97. android:layout_weight=&quot;1&quot;
  98. /&gt;
  99. &lt;Button
  100. android:layout_width=&quot;0dp&quot;
  101. android:layout_height=&quot;fill_parent&quot;
  102. android:layout_weight=&quot;1&quot;
  103. /&gt;
  104. &lt;/LinearLayout&gt;
  105. &lt;/LinearLayout&gt;
  106. &lt;/LinearLayout&gt;

答案4

得分: 0

  1. 您应该按照以下方式使用`TableLayout`
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context=".MainActivity">
  9. <RelativeLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content">
  12. <TextView
  13. android:id="@+id/text_view_p1"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="Player 1 : 0"
  17. android:textSize="30sp" />
  18. <TextView
  19. android:id="@+id/text_view_p2"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_below="@+id/text_view_p1"
  23. android:text="Player 2 : 0"
  24. android:textSize="30sp" />
  25. <Button
  26. android:id="@+id/button_reset"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:layout_alignParentEnd="true"
  30. android:layout_centerVertical="true"
  31. android:layout_marginEnd="41dp"
  32. android:text="reset" />
  33. </RelativeLayout>
  34. <TableLayout
  35. android:layout_width="match_parent"
  36. android:layout_height="0dp"
  37. android:stretchColumns="*"
  38. android:layout_weight="1">
  39. <TableRow>
  40. <Button
  41. android:textSize="60sp" />
  42. <Button
  43. android:textSize="60sp" />
  44. <Button
  45. android:textSize="60sp" />
  46. </TableRow>
  47. <TableRow>
  48. <Button
  49. android:textSize="60sp" />
  50. <Button
  51. android:textSize="60sp" />
  52. <Button
  53. android:textSize="60sp" />
  54. </TableRow>
  55. <TableRow>
  56. <Button
  57. android:textSize="60sp" />
  58. <Button
  59. android:textSize="60sp" />
  60. <Button
  61. android:textSize="60sp" />
  62. </TableRow>
  63. </TableLayout>
  64. </LinearLayout>
英文:

You should use TableLayout as following

  1. &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  2. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  3. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  4. android:layout_width=&quot;match_parent&quot;
  5. android:layout_height=&quot;match_parent&quot;
  6. android:orientation=&quot;vertical&quot;
  7. tools:context=&quot;.MainActivity&quot;&gt;
  8. &lt;RelativeLayout
  9. android:layout_width=&quot;match_parent&quot;
  10. android:layout_height=&quot;wrap_content&quot;&gt;
  11. &lt;TextView
  12. android:id=&quot;@+id/text_view_p1&quot;
  13. android:layout_width=&quot;wrap_content&quot;
  14. android:layout_height=&quot;wrap_content&quot;
  15. android:text=&quot;Player 1 : 0&quot;
  16. android:textSize=&quot;30sp&quot; /&gt;
  17. &lt;TextView
  18. android:id=&quot;@+id/text_view_p2&quot;
  19. android:layout_width=&quot;wrap_content&quot;
  20. android:layout_height=&quot;wrap_content&quot;
  21. android:layout_below=&quot;@+id/text_view_p1&quot;
  22. android:text=&quot;Player 2 : 0&quot;
  23. android:textSize=&quot;30sp&quot; /&gt;
  24. &lt;Button
  25. android:id=&quot;@+id/button_reset&quot;
  26. android:layout_width=&quot;wrap_content&quot;
  27. android:layout_height=&quot;wrap_content&quot;
  28. android:layout_alignParentEnd=&quot;true&quot;
  29. android:layout_centerVertical=&quot;true&quot;
  30. android:layout_marginEnd=&quot;41dp&quot;
  31. android:text=&quot;reset&quot; /&gt;
  32. &lt;/RelativeLayout&gt;
  33. &lt;TableLayout
  34. android:layout_width=&quot;match_parent&quot;
  35. android:layout_height=&quot;0dp&quot;
  36. android:stretchColumns=&quot;*&quot;
  37. android:layout_weight=&quot;1&quot;&gt;
  38. &lt;TableRow&gt;
  39. &lt;Button
  40. android:textSize=&quot;60sp&quot; /&gt;
  41. &lt;Button
  42. android:textSize=&quot;60sp&quot; /&gt;
  43. &lt;Button
  44. android:textSize=&quot;60sp&quot; /&gt;
  45. &lt;/TableRow&gt;
  46. &lt;TableRow&gt;
  47. &lt;Button
  48. android:textSize=&quot;60sp&quot; /&gt;
  49. &lt;Button
  50. android:textSize=&quot;60sp&quot; /&gt;
  51. &lt;Button
  52. android:textSize=&quot;60sp&quot; /&gt;
  53. &lt;/TableRow&gt;
  54. &lt;TableRow&gt;
  55. &lt;Button
  56. android:textSize=&quot;60sp&quot; /&gt;
  57. &lt;Button
  58. android:textSize=&quot;60sp&quot; /&gt;
  59. &lt;Button
  60. android:textSize=&quot;60sp&quot; /&gt;
  61. &lt;/TableRow&gt;
  62. &lt;/TableLayout&gt;
  63. &lt;/LinearLayout&gt;

答案5

得分: 0

根据您的按钮ID,我认为它是:

  1. &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. tools:context=".MainActivity"&gt;
  8. &lt;RelativeLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"&gt;
  11. &lt;TextView
  12. android:id="@+id/text_view_p1"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:text="Player 1 : 0"
  16. android:textSize="30sp" /&gt;
  17. &lt;TextView
  18. android:id="@+id/text_view_p2"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_below="@+id/text_view_p1"
  22. android:text="Player 2 : 0"
  23. android:textSize="30sp" /&gt;
  24. &lt;Button
  25. android:id="@+id/button_reset"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:layout_alignParentEnd="true"
  29. android:layout_centerVertical="true"
  30. android:layout_marginEnd="41dp"
  31. android:text="reset" /&gt;
  32. &lt;/RelativeLayout&gt;
  33. &lt;LinearLayout
  34. android:layout_width="match_parent"
  35. android:layout_height="0dp"
  36. android:layout_weight="1"&gt;
  37. &lt;Button
  38. android:id="@+id/button_00"
  39. android:layout_width="0dp"
  40. android:layout_height="match_parent"
  41. android:layout_weight="1"
  42. android:textSize="60sp" /&gt;
  43. &lt;Button
  44. android:id="@+id/button_01"
  45. android:layout_width="0dp"
  46. android:layout_height="match_parent"
  47. android:layout_weight="1"
  48. android:textSize="60sp" /&gt;
  49. &lt;Button
  50. android:id="@+id/button_02"
  51. android:layout_width="0dp"
  52. android:layout_height="match_parent"
  53. android:layout_weight="1"
  54. android:textSize="60sp" /&gt;
  55. &lt;/LinearLayout&gt;
  56. &lt;LinearLayout
  57. android:layout_width="match_parent"
  58. android:layout_height="0dp"
  59. android:layout_weight="1"&gt;
  60. &lt;Button
  61. android:id="@+id/button_10"
  62. android:layout_width="0dp"
  63. android:layout_height="match_parent"
  64. android:layout_weight="1"
  65. android:textSize="60sp" /&gt;
  66. &lt;Button
  67. android:id="@+id/button_11"
  68. android:layout_width="0dp"
  69. android:layout_height="match_parent"
  70. android:layout_weight="1"
  71. android:textSize="60sp" /&gt;
  72. &lt;Button
  73. android:id="@+id/button_12"
  74. android:layout_width="0dp"
  75. android:layout_height="match_parent"
  76. android:layout_weight="1"
  77. android:textSize="60sp" /&gt;
  78. &lt;/LinearLayout&gt;
  79. &lt;LinearLayout
  80. android:layout_width="match_parent"
  81. android:layout_height="0dp"
  82. android:layout_weight="1"&gt;
  83. &lt;Button
  84. android:id="@+id/button_20"
  85. android:layout_width="0dp"
  86. android:layout_height="match_parent"
  87. android:layout_weight="1"
  88. android:textSize="60sp" /&gt;
  89. &lt;Button
  90. android:id="@+id/button_21"
  91. android:layout_width="0dp"
  92. android:layout_height="match_parent"
  93. android:layout_weight="1"
  94. android:textSize="60sp" /&gt;
  95. &lt;Button
  96. android:id="@+id/button_22"
  97. android:layout_width="0dp"
  98. android:layout_height="match_parent"
  99. android:layout_weight="1"
  100. android:textSize="60sp" /&gt;
  101. &lt;/LinearLayout&gt;
  102. &lt;/LinearLayout&gt;
英文:

Depend your button id. I think it is :

  1. &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  2. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  3. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  4. android:layout_width=&quot;match_parent&quot;
  5. android:layout_height=&quot;match_parent&quot;
  6. android:orientation=&quot;vertical&quot;
  7. tools:context=&quot;.MainActivity&quot;&gt;
  8. &lt;RelativeLayout
  9. android:layout_width=&quot;match_parent&quot;
  10. android:layout_height=&quot;wrap_content&quot;&gt;
  11. &lt;TextView
  12. android:id=&quot;@+id/text_view_p1&quot;
  13. android:layout_width=&quot;wrap_content&quot;
  14. android:layout_height=&quot;wrap_content&quot;
  15. android:text=&quot;Player 1 : 0&quot;
  16. android:textSize=&quot;30sp&quot; /&gt;
  17. &lt;TextView
  18. android:id=&quot;@+id/text_view_p2&quot;
  19. android:layout_width=&quot;wrap_content&quot;
  20. android:layout_height=&quot;wrap_content&quot;
  21. android:layout_below=&quot;@+id/text_view_p1&quot;
  22. android:text=&quot;Player 2 : 0&quot;
  23. android:textSize=&quot;30sp&quot; /&gt;
  24. &lt;Button
  25. android:id=&quot;@+id/button_reset&quot;
  26. android:layout_width=&quot;wrap_content&quot;
  27. android:layout_height=&quot;wrap_content&quot;
  28. android:layout_alignParentEnd=&quot;true&quot;
  29. android:layout_centerVertical=&quot;true&quot;
  30. android:layout_marginEnd=&quot;41dp&quot;
  31. android:text=&quot;reset&quot; /&gt;
  32. &lt;/RelativeLayout&gt;
  33. &lt;LinearLayout
  34. android:layout_width=&quot;match_parent&quot;
  35. android:layout_height=&quot;0dp&quot;
  36. android:layout_weight=&quot;1&quot;&gt;
  37. &lt;Button
  38. android:id=&quot;@+id/button_00&quot;
  39. android:layout_width=&quot;0dp&quot;
  40. android:layout_height=&quot;match_parent&quot;
  41. android:layout_weight=&quot;1&quot;
  42. android:textSize=&quot;60sp&quot; /&gt;
  43. &lt;Button
  44. android:id=&quot;@+id/button_01&quot;
  45. android:layout_width=&quot;0dp&quot;
  46. android:layout_height=&quot;match_parent&quot;
  47. android:layout_weight=&quot;1&quot;
  48. android:textSize=&quot;60sp&quot; /&gt;
  49. &lt;Button
  50. android:id=&quot;@+id/button_02&quot;
  51. android:layout_width=&quot;0dp&quot;
  52. android:layout_height=&quot;match_parent&quot;
  53. android:layout_weight=&quot;1&quot;
  54. android:textSize=&quot;60sp&quot; /&gt;
  55. &lt;/LinearLayout&gt;
  56. &lt;LinearLayout
  57. android:layout_width=&quot;match_parent&quot;
  58. android:layout_height=&quot;0dp&quot;
  59. android:layout_weight=&quot;1&quot;&gt;
  60. &lt;Button
  61. android:id=&quot;@+id/button_10&quot;
  62. android:layout_width=&quot;0dp&quot;
  63. android:layout_height=&quot;match_parent&quot;
  64. android:layout_weight=&quot;1&quot;
  65. android:textSize=&quot;60sp&quot; /&gt;
  66. &lt;Button
  67. android:id=&quot;@+id/button_11&quot;
  68. android:layout_width=&quot;0dp&quot;
  69. android:layout_height=&quot;match_parent&quot;
  70. android:layout_weight=&quot;1&quot;
  71. android:textSize=&quot;60sp&quot; /&gt;
  72. &lt;Button
  73. android:id=&quot;@+id/button_12&quot;
  74. android:layout_width=&quot;0dp&quot;
  75. android:layout_height=&quot;match_parent&quot;
  76. android:layout_weight=&quot;1&quot;
  77. android:textSize=&quot;60sp&quot; /&gt;
  78. &lt;/LinearLayout&gt;
  79. &lt;LinearLayout
  80. android:layout_width=&quot;match_parent&quot;
  81. android:layout_height=&quot;0dp&quot;
  82. android:layout_weight=&quot;1&quot;&gt;
  83. &lt;Button
  84. android:id=&quot;@+id/button_20&quot;
  85. android:layout_width=&quot;0dp&quot;
  86. android:layout_height=&quot;match_parent&quot;
  87. android:layout_weight=&quot;1&quot;
  88. android:textSize=&quot;60sp&quot; /&gt;
  89. &lt;Button
  90. android:id=&quot;@+id/button_21&quot;
  91. android:layout_width=&quot;0dp&quot;
  92. android:layout_height=&quot;match_parent&quot;
  93. android:layout_weight=&quot;1&quot;
  94. android:textSize=&quot;60sp&quot; /&gt;
  95. &lt;Button
  96. android:id=&quot;@+id/button_22&quot;
  97. android:layout_width=&quot;0dp&quot;
  98. android:layout_height=&quot;match_parent&quot;
  99. android:layout_weight=&quot;1&quot;
  100. android:textSize=&quot;60sp&quot; /&gt;
  101. &lt;/LinearLayout&gt;

</LinearLayout>

huangapple
  • 本文由 发表于 2020年7月27日 14:39:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63109920.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定