将视图放在Android中的另一个视图上

时间:2012-08-29 19:31:12

标签: java android android-layout

在我的Android应用程序中,我想在ImageView上添加边框。我不知道如何将视图放在另一个视图上。实际上,边框是一种自定义视图。

自定义视图类:

public class CropBorder extends View {

    private Paint paint = new Paint();


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

    public CropBorder(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }


    @Override
    public void onDraw(Canvas canvas) {
        float[][] circleXY = {{5, 5}, {155, 5}, {305, 5}, {5, 105}, {155, 105}, {305, 105}, {5, 55}, {305, 55}};
        float[][] lineXY = {{5, 5, 305, 5}, {5, 105, 305, 105}, {5, 5, 5, 105}, {305, 5, 305, 105}};

        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(1);

        for(int i = 0 ; i < circleXY.length ; i++)
            canvas.drawCircle(circleXY[i][0], circleXY[i][1], 5, paint);

        paint.setStrokeWidth(2);

        for(int i = 0 ; i < lineXY.length ; i++)
            canvas.drawLine(lineXY[i][0], lineXY[i][1], lineXY[i][2], lineXY[i][3], paint);
    }

}

活动类:

public class CropTestActivity extends Activity {

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

}

布局XML:

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

    <com.nvtech.backend.utility.imageEditor.CropBorder
        android:id="@+id/crop_border"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp" >
    </com.nvtech.backend.utility.imageEditor.CropBorder>

    <ImageView
        android:id="@+id/android_image"
        android:src="@drawable/android"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_gravity="center"
        android:scaleType="fitXY"
        android:contentDescription="@string/android_image_description" >
    </ImageView>

</LinearLayout>

1 个答案:

答案 0 :(得分:2)

您可以将ImageView包装在LinearLayout中,并使用填充来设置边框:

<LinearLayout // this is your root container ... >
    <LinearLayout
        android:id="@+id/content_item_image_wrapper"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#00FFFFFF"
        android:padding="3dip" >

        <ImageView
            android:id="@+id/user_profile_thumbnail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:maxHeight="80dp"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>
</LinearLayout>