嵌套的LinearLayout在Android中不起作用

时间:2018-04-12 21:58:13

标签: android android-layout android-linearlayout nestedlayout

我想在照片中制作layoutProject picture我只能链接它,因为我是新手。当我只用一个LinearLayout运行代码时,它工作正常。但是,当我进行嵌套线性布局时,它只显示一个白色屏幕。而且没有错误。代码根据他们的投掷给团队添加一个分数。谢谢。

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>

2 个答案:

答案 0 :(得分: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"/>

快乐编码

答案 1 :(得分: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替代嵌套视图组。