单击按钮时添加的视图是否显示?

时间:2017-06-04 15:22:43

标签: java android xml android-layout

好的,我现在已经上了6个小时了,出于某种原因,我只是没有得到它。

我正在尝试使用某个公式制作应用来计算某些内容。

现在将其视为平均成绩计算点。我有一个按钮,允许用户在布局中再添加两个微调器,从而继续进行。现在我为此目的制定了一个方法。当我第一次调用这种方法时,会添加微调器。但它第二次没有工作。 如果我将它放在按钮onClick()方法中,它也不起作用。我已经浏览了在线发布的多个示例以及stackoverflow,也许我犯了一些愚蠢的错误。请指导我完成这个。

以下是代码: -

SGPA.class

public class SGPA extends AppCompatActivity {
    View v;
    Context c;
    LinearLayout subjectData;
    ArrayList<Integer> creditIds;
    ArrayList<Integer> gradeIds;
    ArrayList<String> credits;
    ArrayList<String> grades;
    ArrayList<Integer> gradePoints;
    //int id = View.generateViewId();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sgpa);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        credits=new ArrayList<>();
        grades=new ArrayList<>();
        gradePoints=new ArrayList<>();
        //TODO if 2015 or 2013 regulations
        //assuming 2015
        fill2015();
        LayoutInflater inflater = (LayoutInflater) 
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.activity_sgpa, null);
        subjectData=(LinearLayout)v.findViewById(R.id.subjectdata);
        c = this;
        creditIds=new ArrayList<>();
        gradeIds=new ArrayList<>();
        Button button2=(Button)findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addViewForAnotherSubject();
                setContentView(v);
            }
        });
        setContentView(v);
    }
    void addViewForAnotherSubject()
    {
        LinearLayout.LayoutParams lp = new 
LinearLayout.LayoutParams(LinearLayout
                .LayoutParams.MATCH_PARENT, 
LinearLayout.LayoutParams.MATCH_PARENT);
        LinearLayout l=new LinearLayout(c);
        l.setOrientation(LinearLayout.HORIZONTAL);
        l.setLayoutParams(lp);
        l.setGravity(Gravity.CENTER_HORIZONTAL);
        Spinner creditspinner=new Spinner(c);
        Spinner gradespinner=new Spinner(c);
        ArrayAdapter<String> creditSpinnerArrayAdapter = new ArrayAdapter<>
(this,
                android.R.layout.simple_spinner_dropdown_item, credits);
        creditspinner.setAdapter(creditSpinnerArrayAdapter);
        ArrayAdapter<String> gradeSpinnerArrayAdapter = new ArrayAdapter<>
(this,
                android.R.layout.simple_spinner_dropdown_item, grades);
        gradespinner.setAdapter(gradeSpinnerArrayAdapter);
        l.addView(creditspinner);
        l.addView(gradespinner);
        subjectData.addView(l);
    }
    void fill2015()
    {
        grades.add("O");        grades.add("A+");
        grades.add("A");        grades.add("B+");
        grades.add("B");        grades.add("C");
        grades.add("P");        grades.add("F");
        grades.add("Ab");        grades.add("I");
        gradePoints.add(10);        gradePoints.add(9);
        gradePoints.add(8);        gradePoints.add(7);
        gradePoints.add(6);        gradePoints.add(5);
        gradePoints.add(4);        gradePoints.add(0);
        gradePoints.add(0);        gradePoints.add(0);
        for(int i=1;i<=10;i++)
            credits.add(""+i);
    }
    void fill2013()
    {

    }
}

activity_sgpa.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="innominatebit.srmite.SGPA">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_sgpa" />

</android.support.design.widget.CoordinatorLayout>

content_sgpa.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="6dp"
    android:paddingEnd="6dp"
    android:paddingLeft="6dp"
    android:paddingRight="6dp"
    android:paddingStart="6dp"
    android:paddingTop="6dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="innominatebit.srmite.SGPA"
    tools:showIn="@layout/activity_sgpa">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/subjectdata"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/button2"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:gravity="center_horizontal"
            android:orientation="vertical">


        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Button" />

</RelativeLayout>

感谢所有帮助。提前谢谢!

1 个答案:

答案 0 :(得分:0)

好的,我明白了。我做了另一个测试应用程序用于调试目的,直到我得到它。似乎我根本不需要布局充气机。 这是代码:

public class MainActivity extends AppCompatActivity {
    ArrayList<Integer> creditIds;
    ArrayList<Integer> gradeIds;
    ArrayList<String> credits;
    ArrayList<String> grades;
    ArrayList<Integer> gradePoints;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        credits=new ArrayList<>();
        grades=new ArrayList<>();
        gradePoints=new ArrayList<>();
        fill2015();
        creditIds=new ArrayList<>();
        gradeIds=new ArrayList<>();
        final LinearLayout myData=(LinearLayout)findViewById(R.id.myData);
        addforanothersubject(myData);
        Button b1=(Button)findViewById(R.id.button);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            addforanothersubject(myData);
            }
        });
    }
    void addforanothersubject(LinearLayout myData)
    {
        Context c=MainActivity.this;
        LinearLayout.LayoutParams lp = new 
LinearLayout.LayoutParams(LinearLayout
                .LayoutParams.MATCH_PARENT, 
LinearLayout.LayoutParams.MATCH_PARENT);
        LinearLayout l=new LinearLayout(c);
        l.setOrientation(LinearLayout.HORIZONTAL);
        l.setLayoutParams(lp);
        l.setGravity(Gravity.CENTER_HORIZONTAL);
        Spinner creditspinner=new Spinner(c);
        Spinner gradespinner=new Spinner(c);
        ArrayAdapter<String> creditSpinnerArrayAdapter = new ArrayAdapter<>
(c,
                android.R.layout.simple_spinner_item, credits);
        creditspinner.setAdapter(creditSpinnerArrayAdapter);
        ArrayAdapter<String> gradeSpinnerArrayAdapter = new ArrayAdapter<>
(c,
                android.R.layout.simple_spinner_item, grades);
        gradespinner.setAdapter(gradeSpinnerArrayAdapter);
        l.addView(creditspinner);
        l.addView(gradespinner);
        myData.addView(l);
    }
    void fill2015()
    {
        grades.add("O");        grades.add("A+");
        grades.add("A");        grades.add("B+");
        grades.add("B");        grades.add("C");
        grades.add("P");        grades.add("F");
        grades.add("Ab");        grades.add("I");
        gradePoints.add(10);        gradePoints.add(9);
        gradePoints.add(8);        gradePoints.add(7);
        gradePoints.add(6);        gradePoints.add(5);
        gradePoints.add(4);        gradePoints.add(0);
        gradePoints.add(0);        gradePoints.add(0);
        for(int i=1;i<=10;i++)
            credits.add(""+i);
    }
}

xml是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="aaa.test.MainActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <LinearLayout
                android:id="@+id/myData"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
            </LinearLayout>
            <Button
                android:id="@+id/button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:text="Add" />

        </LinearLayout>
    </ScrollView>

</RelativeLayout>

希望它有所帮助。