2

これは私のコードですが、null を返す理由がわかりませんか? ただし、ここに null チェックを入れることはできますが、何か問題はありますか?

    TextView descriptiontv = (TextView) findViewById(R.id.descriptiontv);
    TextView tc = new TextView(c);
    final PopupWindow windowPopUp = new PopupWindow(tc,LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT ,false);

    tc.setBackgroundResource(R.drawable.bg_training_baloon_hc_2x);
    tc.setText("this is a demo test to check how it looks, i m just wanting to test whether it works or not");
    tc.setPadding(20, 20, 20, 20);
    LinearLayout.LayoutParams params = new android.widget.LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    params.setMargins(30, 0, 30, 0);
    tc.setLayoutParams(params);
4

2 に答える 2

2

私の経験に基づいて、次のように、レイアウト パラメータを設定する前に、まずビューをその親ビューに追加する必要があります。

TextView calendarYear = new TextView(getActivity());
calendarYear.setText(calendar.getYear() + "");
calendarYear.setTextSize(20);
calendarYear.setTypeface(null, Typeface.BOLD);
calendarYear.setGravity(Gravity.CENTER);
calendarYear.setTextColor(resources.getColor(android.R.color.black));
calendarItemsLinearLayout.addView(calendarYear);

// We have to add the TextView to the layout first before we
// can set the layout margins for it
ViewGroup.LayoutParams layoutParams = calendarYear.getLayoutParams();
((LinearLayout.LayoutParams)layoutParams).setMargins(0, (int)(16 * density), 0, 0);
calendarYear.setLayoutParams(layoutParams);
于 2015-06-06T09:49:58.950 に答える