質問

SO、クリックオンボタンで作業アプリケーションで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>
.

manactivity.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からすべてを削除し、それが機能します。 しかし、私はこの他の層が私の新しいレイアウトをどのように見せるのを防ぎますか?

役に立ちましたか?

解決 2

親LinearLayoutの誤ったパラメータの問題 - LAYOUT_FOR_SIDES

Android:Orientation="HorizoR"がありますが、Android:Orientation="Vertical"。 そのため、私は何も見たことがない。

他のヒント

これを試してみてください:

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

    }
.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top