在1个活动android中显示2个单独的自定义视图

时间:2013-10-05 16:01:58

标签: android android-fragments android-custom-view

我在1个活动中有2个单独的自定义视图。 我有2个问题:

1-我该怎么做(并排放置2个视图)?

2-如何在显示的后半部分放置第二个视图,这意味着如何使视图考虑(宽度/ 2,0)而不是(0,0)开始? 注意:我的视图高度是显示的高度,我的视图宽度是显示宽度的宽度/ 2。

我像这样设置onMeasure()方法

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int measuredWidth = widthMeasureSpec/2;
    int measuredHeight = heightMeasureSpec;
    setMeasuredDimension(measuredWidth,measuredHeight );
}

display

1 个答案:

答案 0 :(得分:2)

尝试在LinearLayout水平方向内添加两个视图,两者都使用weight=1。它看起来像.-

<LinearLayout
    android:width="match_parent"
    android:height="wrap_content"
    android:orientation="horizontal">

    <YourCustomView1
        android:width="wrap_content"
        android:height="wrap_content"
        android:weight="1" />

    <YourCustomView2
        android:width="wrap_content"
        android:height="wrap_content"
        android:weight="1" />

</LinearLayout>