Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 通过单击按钮创建线性布局_Android_Android Linearlayout_Programmatically Created - Fatal编程技术网

Android 通过单击按钮创建线性布局

Android 通过单击按钮创建线性布局,android,android-linearlayout,programmatically-created,Android,Android Linearlayout,Programmatically Created,因此,我想通过单击按钮在工作应用程序中添加(创建)LinearLayout。我几乎做到了这一点,添加了一些东西(自由空间),但我什么也没看到 为了检查是否添加了布局,我将TextView放入其中 my_layout.xml <LinearLayout android:id="@+id/layout_for_sides" android:layout_width="match_parent" android:layout_height="wrap_content"

因此,我想通过单击按钮在工作应用程序中添加(创建)LinearLayout。我几乎做到了这一点,添加了一些东西(自由空间),但我什么也没看到

为了检查是否添加了布局,我将TextView放入其中

my_layout.xml

<LinearLayout
    android:id="@+id/layout_for_sides"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

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

        <TextView
            android:id="@+id/title_side_size_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1 side size:" />

        <EditText
            android:id="@+id/inp_side_width_1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:hint="cm"
            android:inputType="number" >

            <requestFocus />
        </EditText>

        <EditText
            android:id="@+id/inp_side_height_1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:hint="cm"
            android:inputType="number" >
        </EditText>
    </LinearLayout>
</LinearLayout>

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

    <Button
        android:id="@+id/btn_add_side"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Add side" />

    <Button
        android:id="@+id/btn_calc_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Enter" />
</LinearLayout>
问题已(解决):为什么我什么都没看到?仅显示可用空间,但不显示文本视图

更新:新问题:问题出在具有另一个线性布局的侧面的布局中。 我把所有的东西都从布局中去掉了,这样就可以了。 但是,我不明白这另一层是如何阻止显示我的新布局的?

试试这个:

 public void onClick(View v) {

        switch (v.getId()) {
        case R.id.btn_add_side:
            LinearLayout newlayout = new LinearLayout(this);
            newlayout.setOrientation(LinearLayout.VERTICAL);

            newlayout.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT, 1)); 

           //<---------------------Try this-------------------->
            layout_for_sides.addView(newlayout,new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT, 1));

            LayoutParams lpView = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

            TextView tv = new TextView(this);
   this.newlayout.addView(tv, lpView); //IT WORKS, I SEE FREE SPACE, BUT NEITHER TEXT
                    tv.setText("TextView");
                    tv.setId(5);
                    tv.setTextColor(Color.BLACK);

  newlayout.setBackgroundColor(Color.BLACK); //IT DOESN'T WORK, THERE IS NOTHING WITH BLACK BACKGROUND

            break;

        default:
            break;
        }

    }
public void onClick(视图v){
开关(v.getId()){
案例R.id.btn\u添加侧:
LinearLayout newlayout=新的LinearLayout(此);
新建布局。设置方向(线性布局。垂直);
newlayout.setLayoutParams(新的LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_父级,
LinearLayout.LayoutParams.WRAP_CONTENT,1));
//
用于侧边的布局。添加视图(新建布局,新建LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_父级,
LinearLayout.LayoutParams.WRAP_CONTENT,1));
LayoutParams lpView=新的LayoutParams(LayoutParams.WRAP\u内容,LayoutParams.WRAP\u内容);
TextView tv=新的TextView(此);
this.newlayout.addView(tv,lpView);//它可以工作,我看到了可用空间,但没有看到文本
tv.setText(“文本视图”);
tv.setId(5);
tv.setTextColor(Color.BLACK);
newlayout.setBackgroundColor(Color.BLACK);//它不工作,没有黑色背景
打破
违约:
打破
}
}

父线性布局-布局各侧参数错误的问题

它有android:orientation=“horizontal”,但需要android:orientation=“vertical”。
因此,我什么也没看到。

那么你的问题是什么?对不起,可能不清楚。我刚刚在帖子中添加了一个问题。设置“侧边布局”的高度以匹配侧边布局。不,我在侧边布局下方有另一个布局。如果我这样做,我就看不到了。无论如何,当我点击按钮时,布局中各边的空间会变大,所以,我想,我不需要将匹配父项设置为这个。谢谢,但它不起作用。我理解这个想法——在创建视图之后,在添加参数之前添加视图。和背景色。。。当我尝试“layout_for_sides.addView(tv);”时,文本视图也不会出现。vipul mittal,我复制并粘贴了所有代码,但没有任何更改。不带newlayout.setLayoutParams的P.S
 public void onClick(View v) {

        switch (v.getId()) {
        case R.id.btn_add_side:
            LinearLayout newlayout = new LinearLayout(this);
            newlayout.setOrientation(LinearLayout.VERTICAL);

            newlayout.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT, 1)); 

           //<---------------------Try this-------------------->
            layout_for_sides.addView(newlayout,new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT, 1));

            LayoutParams lpView = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

            TextView tv = new TextView(this);
   this.newlayout.addView(tv, lpView); //IT WORKS, I SEE FREE SPACE, BUT NEITHER TEXT
                    tv.setText("TextView");
                    tv.setId(5);
                    tv.setTextColor(Color.BLACK);

  newlayout.setBackgroundColor(Color.BLACK); //IT DOESN'T WORK, THERE IS NOTHING WITH BLACK BACKGROUND

            break;

        default:
            break;
        }

    }