1

次のように、実行時にレイアウトを追加する必要がある状況があります。

1.Buttonレイアウトは XML で既に定義されています。

XML のボタン レイアウト

2.ユーザーが をクリックするButtonと、EditTextButtonが既存のレイアウトに動的に追加されます。

EditText & 動的に追加された別のボタン

Gingerbread (Android 2.3.5) で正しく動作します。

G レイアウト 1 G レイアウト 2

Kitkat (Android 4.4.2) では動作しません:

K レイアウト 1 K レイアウト 2

誰かがこの問題の回避策を知っていますか? 使用したコードは次のとおりです。

public class MainActivity extends Activity {

private boolean comment = true;

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

    Button button = (Button)findViewById(R.id.commentbutton);
    button.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v){
            if(comment == true){
                comment = false;
                LinearLayout l = (LinearLayout)findViewById(R.id.comment_layout);
                l.setOrientation(LinearLayout.VERTICAL);

                final EditText e = new EditText(v.getContext());
                e.setId(R.id.commentbox);
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(250,100);
                lp.setMargins(0, 20, 0, 0);
                e.setLayoutParams(lp);
                e.setSingleLine(true);
                e.setImeOptions(EditorInfo.IME_ACTION_DONE);
                l.addView(e);

                final Button b = new Button(v.getContext());
                b.setId(R.id.submitbutton);
                lp = new LinearLayout.LayoutParams(150,33);
                lp.setMargins(0, 20, 0, 0);
                b.setLayoutParams(lp);
                b.setText("Submit");
                b.setTextColor(Color.BLACK);
                b.setTypeface(null, Typeface.BOLD);
                b.setBackgroundColor(Color.parseColor("#CCCCCC"));
                b.setFocusable(true);
                b.setFocusableInTouchMode(true);
                l.addView(b);

                e.setOnEditorActionListener(new OnEditorActionListener() {
                    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                        if (actionId == EditorInfo.IME_ACTION_DONE) {
                            b.performClick();
                            return true;
                        }
                        return false;
                    }
                });

                LinearLayout ll = new LinearLayout(v.getContext());
                ll.setId(R.id.productbottomlayout);
                lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,25);
                ll.setLayoutParams(lp);
                ll.setFocusable(true);
                ll.setFocusableInTouchMode(true);
                l.addView(ll);
                ll.requestFocus();

                b.setOnClickListener(new OnClickListener(){
                    @Override
                    public void onClick(View v){
                        LinearLayout l = (LinearLayout) findViewById(R.id.comment_layout);
                        EditText e = (EditText) findViewById(R.id.commentbox);
                        l.removeView(e);
                        Button b = (Button) findViewById(R.id.submitbutton);
                        l.removeView(b);
                        LinearLayout ll = (LinearLayout) findViewById(R.id.productbottomlayout);
                        l.removeView(ll);
                        MainActivity.this.comment = true;
                    }
                });
            }
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

XML レイアウトは次のとおりです。

<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:gravity="center"
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=".MainActivity" >

<LinearLayout 
            android:id="@+id/comment_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginBottom="25dp"
            android:layout_gravity="center"
            android:gravity="center">

            <Button 
                android:id="@+id/commentbutton"
                android:layout_width="150dp"
                android:layout_height="30dp"
                android:text="Add Comment"
                android:textColor="#000000"
                android:textStyle="bold"
                android:background="#CCCCCC"
                android:onClick="showCommentBox"/>

        </LinearLayout>

</RelativeLayout>

また、このようにプログラムでレイアウトを追加する場合、レイアウト要素ごとに手動で ID を設定する必要があります。

私はそれをしました。どうぞ:

<resources>
    <item  type = "id" name = "commentbox"></item>
    <item  type = "id" name = "submitbutton"></item>
    <item type="id" name="productbottomlayout"></item>
</resources>
4

3 に答える 3

1

問題は、ピクセル測定単位で寸法を設定していることです。DP または LayoutParams.WRAP_CONTENT を使用して、アプリをスケーラブルでレスポンシブにします。

だから、それが私のlayoutParamsを設定する方法です:

lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
于 2014-05-23T10:47:12.400 に答える
0

このメソッドを追加し、それを使用してすべてのピクセルを dpi に変換します。

    public static int dpi(int i) {
            Resources r = getResources();
     int value = (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, i,
            r.getDisplayMetrics());
    return value;
}

ソース: https://stackoverflow.com/a/6327095/1977021

于 2014-05-23T10:59:31.153 に答える