LinearLayout
プログラムでいくつかに追加したいと思いTextViews
ます。そして使いたいLayoutInflater
。私は私のアクティビティレイアウトxmlファイルに持っています:
<LinearLayout
android:id="@+id/linear_layout"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
/>
以下のようなアクティビティコードを書きました。
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TextView textView = (TextView) inflater.inflate(R.layout.scale, linearLayout, true);
textView.setText("Some text");
linearLayout.addView(textView);
私のscale.xml
ファイルは次のようになります。
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:drawableTop="@drawable/unit"
/>
行TextView textView = (TextView) inflater.inflate(R.layout.scale, linearLayout, true);
で、以下のような致命的な例外があります。
java.lang.RuntimeException: Unable to start activity ComponentInfo{my.package/my.package.MyActivity}:
java.lang.ClassCastException: android.widget.LinearLayout
Caused by: java.lang.ClassCastException: android.widget.LinearLayout
問題のある行をnullに置き換えるとlinearLayout
、例外はありませんが、私のandroid:layout_marginLeft
とは無視され、追加されたTextViewの周りにマージンが表示されません。android:layout_marginRight
scale.xml
ExpandableListView にヘッダー ビューを追加するときに Android: ClassCastExceptionという質問が見つかりましたが、私の場合、インフレータを使用する最初の行に例外があります。