【问题标题】:How to use viewGroup in android [closed]如何在android中使用viewGroup [关闭]
【发布时间】:2012-07-24 23:12:20
【问题描述】:

如何在android中使用viewGroup

【问题讨论】:

  • Here 是来自 Google 的文档
  • about-android.blogspot.com/2010/05/… 这个例子可以。在ListView 中使用此类视图时,请务必为此类视图设置唯一的 id-s,否则它们可以在滚动时重绘
  • ViewGroup 是字面上的任何一组视图,-ListViewLinear/Horizontal/VerticalLayout ...,所有这些类型。你要什么例子?添加或删除视图?
  • 感谢您的链接!很有帮助!

标签: android


【解决方案1】:

如前所述,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);
    }
}

【讨论】:

  • 这里的 MyViewGroup 类什么也不做,什么也不打印。
  • 我认为这个答案对其他人没有任何帮助。由于它扩展了LinearLayout,并且所有那些应该被覆盖的工作都被抛出到LinearLayout。这毫无意义。
  • 这个想法是通过占位符代码显示 ViewGroup 是什么。不多也不少。
  • 你能解释一下 onLayout() 方法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-25
  • 2012-09-29
  • 2016-11-18
  • 2014-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多