programing

Android: 선형 레이아웃에 테두리를 그리는 방법

bestprogram 2023. 7. 31. 21:34

Android: 선형 레이아웃에 테두리를 그리는 방법

파일이 세 개 있습니다.XML, 그리기 기능 및 기본 활동.저는 조금 가지고 있어요.LinearLayout내 XML 파일에 있습니다.

<LinearLayout android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_weight="1">
    <LinearLayout android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:background="#ef3"
                  android:id="@+id/img01"/>
    <LinearLayout android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:background="#E8A2B4"
                  android:id="@+id/img02"/>
</LinearLayout>

그리기 기능은 다음과 같습니다.

public class getBorder extends TextView {
    public getBorder(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();

        paint.setColor(android.graphics.Color.RED);

        canvas.drawLine(0, 0, this.getWidth() - 1, 0, paint);
        canvas.drawLine(0, 0, 0, this.getHeight() - 1, paint);
        canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1,
            this.getHeight() - 1, paint);
        canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1,
            this.getHeight() - 1, paint);
    }
}

다음은 주요 활동입니다.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final getBorder getBorder = new getBorder(this);
    final LinearLayout img01 = (LinearLayout) findViewById(R.id.img01);
    img01.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            getBorder.setWidth(100);
            getBorder.setHeight(100);
            img01.addView(getBorder);
        }
    });       
}

프로그램이 테두리를 그릴 수 있지만 크기가 맞지 않습니다.LinearLayout그리고 내가 클릭하면LinearLayout다시, 그 프로그램은 충돌했습니다.

또한, 저는 중앙에 두 개의 원을 그리고 싶습니다.LinearLayout하지만 어떻게 중심 좌표를 알아낼 수 있죠?

정말 프로그래밍 방식으로 해야 합니까?

제목만 생각하면 다음과 같습니다.ShapeDrawable을 Android:background로 사용할 수 있습니다.

예를 들어, 다음과 같이 정의합니다.res/drawable/my_custom_background.xml다음과 같이:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
  <corners
      android:radius="2dp"
      android:topRightRadius="0dp"
      android:bottomRightRadius="0dp"
      android:bottomLeftRadius="0dp" />
  <stroke
      android:width="1dp"
      android:color="@android:color/white" />
</shape>

Android:background="@drawable/my_custom_background"를 정의합니다.

저는 테스트를 하지 않았지만 작동할 것입니다.

업데이트:

당신의 요구에 부합한다면 xml 모양의 그리기 가능한 자원 파워를 활용하는 것이 더 낫다고 생각합니다."처음부터" 프로젝트(Android-8용)를 사용하여 res/layout/main.xml을 정의합니다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/border"
    android:padding="10dip" >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World, SOnich"
        />
    [... more TextView ...]
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World, SOnich"
        />
</LinearLayout>

그리고 ares/drawable/border.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
   <stroke
        android:width="5dip"
        android:color="@android:color/white" />
</shape>

진저브레드 장치에서 작동하는 것으로 보고되었습니다.참고: 관련 정보가 필요합니다.android:padding의 선형 레이아웃.android:width형상/획량 값사용하지 마십시오.@android:color/white최종 응용 프로그램에서 사용할 수 있지만 프로젝트 정의 색상입니다.

신청할 수 있습니다.android:background="@drawable/border" android:padding="10dip"제공된 샘플에서 각 선형 레이아웃으로 이동합니다.

일부 원을 선형 레이아웃의 배경으로 표시하는 것과 관련된 다른 게시물에 대해서는 선형 레이아웃의 배경에 완벽한 원을 표시하기 위해 삽입/스케일/레이어 그리기 가능한 리소스(자세한 내용은 그리기 가능한 리소스 참조)를 사용하고 있지만 현재 실패했습니다.

당신의 문제는 분명히 사용하는 것에 있습니다.getBorder.set{Width,Height}(100);onClick 방식으로 하는 이유는 무엇입니까?

요점을 놓치지 않으려면 추가 정보가 필요합니다. 왜 프로그래밍 방식으로 그렇게 합니까?역동적인 행동이 필요합니까?입력 그리기 가능한 그리기 가능한 것이 png 또는 ShapeDrawable입니까?기타.

계속 진행(아마도 내일이나 달성하고자 하는 목표에 대해 더 정확한 정보를 제공하는 즉시)…

선형 레이아웃/상대 레이아웃을 확장하고 XML에서 바로 사용

package com.pkg_name ;
...imports...
public class LinearLayoutOutlined extends LinearLayout {
    Paint paint;    

    public LinearLayoutOutlined(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        setWillNotDraw(false) ;
        paint = new Paint();
    }
    public LinearLayoutOutlined(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        setWillNotDraw(false) ;
        paint = new Paint();
    }
    @Override
    protected void onDraw(Canvas canvas) {
        /*
        Paint fillPaint = paint;
        fillPaint.setARGB(255, 0, 255, 0);
        fillPaint.setStyle(Paint.Style.FILL);
        canvas.drawPaint(fillPaint) ;
        */

        Paint strokePaint = paint;
        strokePaint.setARGB(255, 255, 0, 0);
        strokePaint.setStyle(Paint.Style.STROKE);
        strokePaint.setStrokeWidth(2);  
        Rect r = canvas.getClipBounds() ;
        Rect outline = new Rect( 1,1,r.right-1, r.bottom-1) ;
        canvas.drawRect(outline, strokePaint) ;
    }

}

<?xml version="1.0" encoding="utf-8"?>

<com.pkg_name.LinearLayoutOutlined
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
    android:layout_width=...
    android:layout_height=...
   >
   ... your widgets here ...

</com.pkg_name.LinearLayoutOutlined>

언급URL : https://stackoverflow.com/questions/8203606/android-how-to-draw-a-border-to-a-linearlayout