android - 扩展 View 组

标签 android android-layout

public SomeClass(Context context, SomeType some_arg) {
         super(context);
         LayoutInflater inflater = (LayoutInflater)context.getSystemService(
                  Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.some_layout_with_button, this, true);
        m_button=(Button)findViewById(R.id.mButton);
        m_textView=(TextView)findViewById(R.id.mTextView);
        m_button.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v){
                //TODO
                }
            });
    }

在Activity中,在onCreate(...)中我写了

this.setContentView(new SomeClass(getApplicationContext(),arg));

SomeClass extends LinearLayout, some_layout_with_button.xml 是正确的 但在模拟器的显示屏上 - 什么都没有。为什么?

更新:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <LinearLayout android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="20px">
        <Button android:id="@+id/mButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="BT" />
        <TextView android:id="@+id/mTextView"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textSize="16px"
                />
    </LinearLayout>
    <LinearLayout android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
        <RadioGroup android:id="@+id/mRadioGroup"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
            <RadioButton android:id="@+id/mRadioButton1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="60px"
                    android:text="Text1"/>
            <RadioButton android:id="@+id/mRadioButton2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="60px"
                    android:text="Text2"/>
        </RadioGroup>
    </LinearLayout>
</LinearLayout>

最佳答案

我想打几个电话给http://developer.android.com/reference/android/view/ViewGroup.html#addView(android.view.View,%20int ) 在你的构造函数代码中丢失了

关于android - 扩展 View 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4294197/

相关文章:

android - 我如何从android中的url解析xml?

Android Studio 在 xml 文件中显示一些困惑

android - Retrofit - 在 GET 请求中以 "/"形式传递参数

java.lang.NoClassDefFoundError : android. support.v7.mediarouter.R$string

android - 在 scrollView android 上实现缩放

android - 按钮 setOnClickListener 不起作用

Android:如何在styles.xml 中自定义声明XML 命名空间?

android - 在 LinearLayout 中使用 Gridview 和 Button

android - Activity 转换 : Performing stop of activity that is not resumed

android - 任务 ':google_api_headers:compileDebugKotlin' 执行失败