【问题标题】:Creating LinearLayout by the click on Button通过单击按钮创建 LinearLayout
【发布时间】:2014-08-01 07:12:23
【问题描述】:

所以,我想通过单击按钮在工作应用程序中添加(创建)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>

MainActivity.java

public class MainActivity extends Activity implements OnClickListener {
    Button btn_add_side;
    LinearLayout layout_for_sides;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        layout_for_sides = (LinearLayout) findViewById(R.id.layout_for_sides);

        btn_add_side = (Button) findViewById(R.id.btn_add_side);

        btn_add_side.setOnClickListener(this);
    }
    @Override
    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)); 

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

            layout_for_sides.addView(newlayout);

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

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

            break;

        default:
            break;
        }

    }
}

问题是(已解决): 为什么我什么都没看到?只出现空闲空间,但没有 TextVeiw。

更新: 新问题:问题出在具有另一个 LinearLayout 的 layout_for_sides 中。 我从 layout_for_sides 中删除了所有内容,并且可以正常工作。 但是,我不明白这个另一层如何阻止显示我的新布局?

【问题讨论】:

  • 那么你的问题是什么?
  • 抱歉,可能不清楚。我刚刚在帖子中添加了问题。
  • 将“layout_for_sides”的高度设置为match_parent
  • TheOnlyJakobob,不,我在 layout_for_sides 下面还有另一个布局。如果我这样做,我看不到它。无论如何,当我单击 Button 时,layout_for_sides 中的空间变得更高,所以,我认为,我不需要将 match_parent 设置为此。

标签: android android-linearlayout programmatically-created


【解决方案1】:

试试这个:

 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;
        }

    }

【讨论】:

  • 谢谢,但它不起作用。我理解这个想法 - 在创建视图之后,在添加参数之前添加视图。和背景色......当我尝试“layout_for_sides.addView(tv);” TextView 也没有出现。
  • vipul mittal,我复制并粘贴了所有代码,但没有任何改变。附言没有 newlayout.setLayoutParams
【解决方案2】:

父LinearLayout参数错误的问题 - layout_for_sides。

它有 android:orientation="horizo​​ntal",但需要 android:orientation="vertical"。 因此我什么都没看到。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-15
    • 2016-03-01
    • 1970-01-01
    • 2020-10-04
    相关资源
    最近更新 更多