android - 如何在android中使用viewGroup

标签 android

如何在android中使用viewGroup

最佳答案

如前所述,ViewGroup 是所有 ViewGroup 扩展的抽象类,例如 LinearLayout 就是一个 ViewGroup。

MyViewGroup.java:

public class MyViewGroup extends LinearLayout {

    public MyViewGroup(Context context) {
        super(context);
    }

    public MyViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyViewGroup(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        Log.e("SWIPED", "onLayout : " + Boolean.toString(changed));
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        super.onInterceptTouchEvent(event);
        Log.e("SWIPED", "onInterceptTouchEvent : " + event.getAction());
        return false;
    }
}

Main.XML:

<com.example.MyViewGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/screen" android:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="fill_parent">

<TextView android:id="@+id/tv1" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:text="TEXT ONE"
    android:padding="20dip" android:background="@android:color/background_dark" />

<TextView android:padding="20dip" android:background="@android:color/background_light"
    android:id="@+id/tv2" android:layout_height="wrap_content"
    android:text="TEXT TWO" android:layout_width="fill_parent" />

</com.example.MyViewGroup>

MainActivity.java:

public class MainActivity extends Activity {

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

关于android - 如何在android中使用viewGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1367983/

相关文章:

android - 在应用更新中在 Google Play Core 中安装错误(-100)

android - BitmapFactory.decodeResource 和莫名其妙的Out of Memory

Android设置View.GONE在listview中没有 "release"空间

java - 在 android 上播放本地 m3u8 文件显示 setDataSourceFD 失败。 : status=0x80000000

android - 当我在手机(android 6.1 和 api 23)上运行我的应用程序时它崩溃了,但是当我在模拟器(android 7.0 和 api 24)上运行它时它不会崩溃

android - 更新到 support-v7 :27. 1.0 时 Activity 状态栏为黑色而不是透明

android - 如何在 GridView 中编辑文本域(EditText)?数独解决应用程序

android - Gradle 项目同步失败。 Osmdroid

android - ruby 试运行没有这样的文件或目录/platform-tools/adb (Errno::ENOENT)

android - 如何在 android 中验证单选按钮的性别?