从Android

时间:2016-07-19 16:40:57

标签: android android-layout layout

您好我在TableRow添加TextView和代码中的按钮时遇到问题。我在StackOverflow上阅读了很多答案,但仍然没有用。我的主要目标是为textview和按钮设置权重,但点击按钮后这里是零动作。这是我的代码:

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.zawadeusz.to_do_list.List_Activity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/scrollView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" >


        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/Table">

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

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="TEXT"
                    android:id="@+id/textView2"
                    android:layout_weight=".9" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="DONE"
                    android:layout_weight=".1"
                    android:id="@+id/DoneBtn"
                    android:layout_column="12"
                    android:onClick="Done"/>
            </TableRow>
        </TableLayout>
    </ScrollView>
</RelativeLayout>

这是我的java代码:

public class List_Activity extends AppCompatActivity {

    TextView TaskTextView;
    Button DoneBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

        DoneBtn = (Button) findViewById(R.id.DoneBtn);
        TaskTextView = (TextView)findViewById(R.id.textView2);

        Intent intent = getIntent();

        String message = intent.getStringExtra("TASK");

        TaskTextView.setText(message);
    }

    public void Done(View view)
    {
        TableLayout tbl = (TableLayout)findViewById(R.id.Table);

        TableLayout.LayoutParams params = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT,1.0f);
        TableLayout.LayoutParams params1 = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT,.9f);
        TableLayout.LayoutParams params2 = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT,.1f);

        TableRow row = new TableRow(this);
        TextView task = new TextView(this);
        Button done = new Button(this);

        row.setLayoutParams(params);
        task.setLayoutParams(params1);
        done.setLayoutParams(params2);


        task.setText("Textview");
        done.setText("Button");

        row.addView(task);
        row.addView(done);
        tbl.addView(row);

    }
}

0 个答案:

没有答案