1

我正在尝试layout像图片中那样。项目图片我只能链接它,因为我是新人。当我只用一个运行代码时,LinearLayout它工作正常。但是当我进行嵌套线性布局时,它只显示一个白色屏幕。并且没有错误。该代码根据他们的投掷s为团队添加分数。谢谢。

Java代码:

public class MainActivity extends AppCompatActivity {
    int scorea=0;
    int scoreb=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }

    @Override
    public 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;
    }

    @Override
    public 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 SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    public void displayforpointb3(View view){
        TextView scoreView=(TextView)findViewById(R.id.team_b_score);
        scoreb+=3;
        scoreView.setText(""+scoreb);
    }
    public void displayforpointb2(View view){
        TextView scoreView=(TextView)findViewById(R.id.team_b_score);
        scoreb+=3;
        scoreView.setText(""+scoreb);
    }
    public void displayforfreethrowb(View view){
        TextView scoreView=(TextView)findViewById(R.id.team_b_score);
        scoreb++;
        scoreView.setText(""+scoreb);
    }
    public void displayforpoint3(View view){
        TextView scoreView=(TextView)findViewById(R.id.team_a_score);
        scorea+=3;
        scoreView.setText(""+scorea);
    }
    public void displayforpoint2(View view){
        TextView scoreView=(TextView)findViewById(R.id.team_a_score);
        scorea+=2;
        scoreView.setText(""+scorea);
    }
    public void displayforfreethrow(View view){
        TextView scoreView=(TextView)findViewById(R.id.team_a_score);
        scorea++;
        scoreView.setText(""+scorea);
    }

}

XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.example.android.courtcounter.MainActivity"
    android:orientation="horizontal">

    <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:minHeight="?attr/actionBarSize"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:titleTextColor="@android:color/white"
    android:background="?attr/colorPrimary"/>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Teama"
            android:textSize="14dp"
            android:layout_gravity="center_horizontal"
            android:padding="8dp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="14dp"
            android:text="@string/zero"
            android:layout_gravity="center_horizontal"
            android:padding="8dp"
            android:id="@+id/team_a_score" />
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:padding="8dp"
            android:text="@string/point3"
            android:onClick="displayforpoint3"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:padding="8dp"
            android:text="@string/point2"
            android:onClick="displayforpoint2"/>
    <Button
        android:layout_width="match_parent"
        android:layout_gravity="center_horizontal"
        android:padding="8dp"
        android:layout_height="wrap_content"
        android:text="@string/freethrow"
        android:onClick="displayforfreethrow"/>
    </LinearLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Teama"
            android:textSize="14dp"
            android:layout_gravity="center_horizontal"
            android:padding="8dp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="14dp"
            android:layout_gravity="center_horizontal"
            android:padding="8dp"
            android:text="@string/zero"
            android:id="@+id/team_b_score" />
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_margin="8dp"
            android:text="@string/point3"
            android:onClick="displayforpointb3"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:text="@string/point2"
        android:layout_weight="1"/>
        android:onClick="displayforpointb2"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:padding="8dp"
        android:text="@string/freethrow"
        android:onClick="displayforfreethrowb"/>
    </LinearLayout>
</LinearLayout>
4

2 回答 2

0

首先,您应该TextView在您的类中定义您的私有属性,如下所示

private TextView scoreViewA;
private TextView scoreViewB;

然后在您的onCreate()方法中,您应该找到TextView如下视图

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        scoreViewA = findViewById(R.id.team_a_score);
        scoreViewB = findViewById(R.id.team_b_score);
    }

然后在你的onClick方法中,你应该更新那些TextView

public void displayforpointb3(View view){
        scoreb+=3;
        scoreViewB.setText(""+scoreb);
    }
    public void displayforpointb2(View view){
        scoreb+=2; //You had scoreb+=3 I changed it to 2
        scoreViewB.setText(""+scoreb);
    }
    public void displayforfreethrowb(View view){
        scoreb++;
        scoreViewB.setText(""+scoreb);
    }
    public void displayforpoint3(View view){
        scorea+=3;
        scoreViewA.setText(""+scorea);
    }
    public void displayforpoint2(View view){
        scorea+=2;
        scoreViewA.setText(""+scorea);
    }
    public void displayforfreethrow(View view){
        scorea++;
        scoreViewA.setText(""+scorea);
    }

我也看到你的错误,activity_main.xml我猜是错误类型

<Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:text="@string/point2"
        android:layout_weight="1"/>  <--- Remove this "/>"
        android:onClick="displayforpointb2"/>

快乐编码

于 2018-04-12T23:05:33.407 回答
0

问题

根的方向LinearLayout是水平的。这导致子视图出现在右侧Toolbar而不是其下方。

解决方案

使根LinearLayout垂直并将两个内部视图组包裹在一个水平的LinearLayout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <!-- Team A -->
        <LinearLayout
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="vertical">
            
            <!-- Text & Buttons -->
            
        </LinearLayout>
        
        <!-- Team B -->
        <LinearLayout
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <!-- Text & Buttons -->

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

它不漂亮,但它完成了工作。ConstraintLayout如果遇到任何性能问题,请考虑使用嵌套视图组的替代方法。

于 2018-04-13T06:04:18.330 回答