以编程方式在textview旁边添加按钮

时间:2019-07-05 03:44:53

标签: android textview

目前,我一直在代码中以编程方式添加textview。

我想要的是以编程方式在“ anothertextview”旁边放置一个按钮

这是我当前的代码:

  void addSubCat(){
        final TextView subCat = new TextView(EncodeGroupActivity.this);
        subCat.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        subCat.setText("Sub Category");
        subCat.setTextSize(12);
        subCat.setId(subCatCounter + 1);

        final TextView subCatTextField = new TextView(EncodeGroupActivity.this,null,0,R.style.TextViewEncode);
        subCat.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        subCatTextField.setTextAppearance(EncodeGroupActivity.this, R.style.TextViewEncode);
        subCatTextField.setHint(R.string.sub_category);
        subCatTextField.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSubCategoryDialog(subCatTextField);
            }
        });
        subCatTextField.setId(subCat.getId());


        linearLayoutInside.addView(subCat);
        linearLayoutInside.addView(subCatTextField);
    }

这里是我想要实现的示例可视化。

2 个答案:

答案 0 :(得分:2)

1。=>嗨!首先在您的XML文件中采用一个Vertical Vertical的LinearLayout。.

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

2。=>在活动中添加以下代码(作为方法中的问题)

private LinearLayout mainLayout;
        mainLayout=findViewById(R.id.mainLayout);

        LinearLayout linearLayout=new LinearLayout(MainActivity.this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        mainLayout.addView(linearLayout);
        TextView textView=new TextView(MainActivity.this);
        textView.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        textView.setText("TEXTVIEW");
        textView.setTextSize(12);
        textView.setId(1);

        linearLayout.addView(textView);

        LinearLayout linearLayout1=new LinearLayout(MainActivity.this);
        linearLayout1.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout1.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mainLayout.addView(linearLayout1);
        TextView textView1=new TextView(MainActivity.this);
        textView1.setLayoutParams(new LinearLayout.LayoutParams(
                400,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        textView1.setText("ANOTHERTEXTVIEW");
        textView1.setTextSize(12);
        textView1.setId(2);
        linearLayout1.addView(textView1);


        Button button=new Button(MainActivity.this);
        button.setLayoutParams(new LinearLayout.LayoutParams(
                300,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        button.setText("BUTTON");
        button.setPadding(10,10,10,10);
        button.setTextSize(12);
        linearLayout1.addView(button);

然后根据需要最终获得您的视图。;;)enter image description here

答案 1 :(得分:0)

尝试

void addSubCat(){
            final TextView subCat = new TextView(EncodeGroupActivity.this);
            subCat.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT));
            subCat.setText("Sub Category");
            subCat.setTextSize(12);
            subCat.setId(subCatCounter + 1);

            final TextView subCatTextField = new TextView(EncodeGroupActivity.this,null,0,R.style.TextViewEncode);
            subCat.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT));
            subCatTextField.setTextAppearance(EncodeGroupActivity.this, R.style.TextViewEncode);
            subCatTextField.setHint(R.string.sub_category);
            subCatTextField.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showSubCategoryDialog(subCatTextField);
                }
            });
            subCatTextField.setId(subCat.getId());

           Button myButton = new Button(this);<-------
           myButton.setText("Push Me");<-------
           linearLayoutInside.addView(myButton, lp);<-------

            linearLayoutInside.addView(subCat);
            linearLayoutInside.addView(subCatTextField);
        }