Programing

XML 드로어 블을 사용하는 세로선

lottogame 2020. 6. 15. 08:21
반응형

XML 드로어 블을 사용하는 세로선


드로어 블로 사용할 수직선 (1dp 두께)을 정의하는 방법을 알아 내려고합니다.

수평을 만들려면 매우 간단합니다.

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line">
    <stroke android:width="1dp" android:color="#0000FF"/>
    <size android:height="50dp" />     
</shape>

문제는이 선을 수직으로 만드는 방법입니다.

예, 1px 두께의 사각형 모양을 그리는 것과 같은 해결 방법이 있지만 드로어 블 XML이 여러 <item>요소 로 구성된 경우 복잡해집니다 .

누구든지이 기회가 있었습니까?

최신 정보

사건은 여전히 ​​해결되지 않았습니다. 그러나 Android 설명서 십자군을 사용하는 사람에게는 다음과 같은 유용한 정보가 있습니다. Android XML 매뉴얼 누락

최신 정보

내가 올바른 것으로 표시 한 방법 이외의 다른 방법을 찾지 못했습니다. 트릭은 약간 "무거운"느낌이지만, 답변을 알고 있다면 공유하는 것을 잊지 마십시오.)


모양 대신 다음을 시도 할 수 있습니다 View.

<View
    android:layout_width="1dp"
    android:layout_height="fill_parent"
    android:background="#FF0000FF" />

나는 이것을 수평선에만 사용했지만 수직선에도 효과가 있다고 생각합니다.

사용하다:

<View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="#FF0000FF" />

수평선.


회전 태그 안에 도형을 중첩시킬 수 있습니다.

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="90"
    android:toDegrees="90">
    <shape 
        android:shape="line">
        <stroke
            android:width="1dp"
            android:color="#ff00ff"
            android:dashWidth="1dp"
            android:dashGap="2dp" />
    </shape>
</rotate>

그러나 유일한 문제는 레이아웃 xml에 정의 된 레이아웃 매개 변수가 원래 모양을 그리는 데 사용되는 크기입니다. 행의 높이가 30dp가되도록하려면 레이아웃 xml에서 layout_width를 30dp로 정의해야합니다. 그러나이 경우 최종 폭도 30dp가되므로 대부분의 상황에서 바람직하지 않습니다. 이것은 본질적으로 너비와 높이가 모두 동일한 값, 즉 원하는 선 길이 값이어야 함을 의미합니다. 이 문제를 해결하는 방법을 알 수 없었습니다.

이것은 "안드로이드 방식"솔루션 인 것처럼 보이지만 언급 한 차원 문제에 대한 해결 방법이 없으면 대부분의 사람들에게는 효과가 없을 것입니다. 실제로 필요한 것은 <shape /> 또는 <stroke />의 방향 속성입니다.

회전 태그의 속성에서 다음과 같은 다른 드로어 블을 참조 할 수도 있습니다.

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="90"
    android:toDegrees="90"
    android:drawable="@drawable/horizontal_line" />

그러나 나는 이것을 테스트하지 않았으며 동일한 문제가있을 것으로 기대합니다.

-- 편집하다 --

아, 나는 실제로 고침을 알아 냈습니다. 레이아웃 XML에서 음수 여백을 사용하여 원하지 않는 추가 공간을 제거 할 수 있습니다. 같은 :

<ImageView
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:layout_marginLeft="-15dp"
    android:layout_marginRight="-15dp"
    android:src="@drawable/dashed_vertical_line" />

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

일은 나를위한 것입니다. fill_parent 또는 dp 높이의 고정 크기를 사용하여 배경의 배경으로 배치하십시오.


회전 속성을 사용할 수 있습니다

 <item>
    <rotate
        android:fromDegrees="90"
        android:toDegrees="90"
        android:pivotX="50%"
        android:pivotY="50%" >
        <shape
            android:shape="line"
            android:top="1dip" >
            <stroke
                android:width="1dip"
                 />
        </shape>
    </rotate>
</item>

나는 다른 해결책을 생각해 냈습니다. 아이디어는 선을 원하는 색상으로 드로어 블을 먼저 채우고 왼쪽 또는 오른쪽 패딩을 사용하는 동안 전체 영역을 배경색으로 다시 채우는 것입니다. 분명히 이것은 드로어 블의 가장 왼쪽 또는 오른쪽의 세로선에만 적용됩니다.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@color/divider_color" />
    <item android:left="6dp" android:drawable="@color/background_color" />
</layer-list>

이것이 가장 간단한 해결책이라고 생각합니다.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:gravity="center">
        <shape android:shape="rectangle">
            <size android:width="1dp" />
            <solid android:color="#0000FF" />
        </shape>
    </item>

</layer-list>

뷰를 동적 / 프로그래밍 방식으로 추가해야하므로 추가 뷰를 추가하는 것은 번거로 웠습니다. 뷰 높이는 WRAP_CONTENT이므로 사각형 솔루션을 사용할 수 없습니다. 나는 블로그 포스트 발견 여기에 내가 그 구현 있도록 된 onDraw를 오버라이드 (override), 텍스트 뷰를 확장 ()와 라인에 그림에 대한을하고 그것을 잘 작동합니다. 아래 코드를 참조하십시오 :

public class NoteTextView extends TextView {
    public NoteTextView(Context context) {
       super(context);
    }
    private Paint paint = new Paint();
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(Color.parseColor("#F00000FF"));
        paint.setStrokeWidth(0);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawLine(0, 0, 0, getHeight(), paint);
    }
}

왼쪽에 세로선이 필요했지만 drawLine(startX, startY, stopX, stopY, paint)드로우 라인 매개 변수는 뷰에서 어느 방향 으로든 직선을 그릴 수 있습니다. 그런 다음 내 활동에서 NoteTextView note = new NoteTextView(this);이것이 도움이되기를 바랍니다.


매우 간단합니다 ... 안드로이드 XML에 수직선을 추가하는 것 ...

<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginTop="5dp"
android:rotation="90"
android:background="@android:color/darker_gray"/>

세로선이 필요한 위치에 따라 다르지만 예를 들어 세로 테두리가 필요한 경우 부모 뷰에 배경을 사용자 정의 드로어 블로 가질 수 있습니다. 그런 다음 드로어 블을 다음과 같이 정의 할 수 있습니다.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape
            android:shape="rectangle">
            <stroke android:width="1dp" android:color="#000000" />
            <solid android:color="#00ffffff" />

        </shape>
    </item>

    <item android:right="1dp">
        <shape android:shape="rectangle">
            <solid android:color="#00ffffff" />
        </shape>
    </item>

</layer-list>

이 예제는 뷰의 오른쪽에 1dp가는 검은 선을 만들며,이 드로어 블을 배경으로 사용합니다.


모양을 사용할 수 있지만 선 대신 사각형으로 만듭니다.

android:shape="rectangle">
<stroke
    android:width="5dp"
    android:color="#ff000000"
    android:dashGap="10px"
    android:dashWidth="30px" />

그리고 레이아웃에서 이것을 사용하십시오 ...

<ImageView
    android:layout_width="7dp"
    android:layout_height="match_parent"
    android:src="@drawable/dashline"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layerType="software"/>

대시의 크기에 따라 너비를 가지고 놀아 한 줄로 만들어야 할 수도 있습니다.

이것이 건배에 도움이되기를 바랍니다.


add this in your styles.xml

        <style name="Divider">
            <item name="android:layout_width">1dip</item>
            <item name="android:layout_height">match_parent</item>
            <item name="android:background">@color/divider_color</item>
        </style>

        <style name="Divider_invisible">
            <item name="android:layout_width">1dip</item>
            <item name="android:layout_height">match_parent</item>
        </style>

then wrap this style in a linear layout where you want the vertical line, I used the vertical line as a column divider in my table. 

     <TableLayout
                android:id="@+id/table"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:stretchColumns="*" >

                <TableRow
                    android:id="@+id/tableRow1"
                    android:layout_width="fill_parent"
                    android:layout_height="match_parent"
                    android:background="#92C94A" >

                    <TextView
                        android:id="@+id/textView11"
                        android:paddingBottom="10dp"
                        android:paddingLeft="5dp"
                        android:paddingRight="5dp"
                        android:paddingTop="10dp" />
    //...................................................................    

                    <LinearLayout
                        android:layout_width="1dp"
                        android:layout_height="match_parent" >

                        <View style="@style/Divider_invisible" />
                    </LinearLayout>
        //...................................................................
                    <TextView
                        android:id="@+id/textView12"
                        android:paddingBottom="10dp"
                        android:paddingLeft="5dp"
                        android:paddingRight="5dp"
                        android:paddingTop="10dp"
                        android:text="@string/main_wo_colon"
                        android:textColor="@color/white"
                        android:textSize="16sp" />
  //...............................................................  
                    <LinearLayout
                        android:layout_width="1dp"
                        android:layout_height="match_parent" >

                        <View style="@style/Divider" />
                    </LinearLayout>

    //...................................................................
                    <TextView
                        android:id="@+id/textView13"
                        android:paddingBottom="10dp"
                        android:paddingLeft="5dp"
                        android:paddingRight="5dp"
                        android:paddingTop="10dp"
                        android:text="@string/side_wo_colon"
                        android:textColor="@color/white"
                        android:textSize="16sp" />

                    <LinearLayout
                        android:layout_width="1dp"
                        android:layout_height="match_parent" >

                        <View style="@style/Divider" />
                    </LinearLayout>

                    <TextView
                        android:id="@+id/textView14"
                        android:paddingBottom="10dp"
                        android:paddingLeft="5dp"
                        android:paddingRight="5dp"
                        android:paddingTop="10dp"
                        android:text="@string/total"
                        android:textColor="@color/white"
                        android:textSize="16sp" />
                </TableRow>

                <!-- display this button in 3rd column via layout_column(zero based) -->

                <TableRow
                    android:id="@+id/tableRow2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#6F9C33" >

                    <TextView
                        android:id="@+id/textView21"
                        android:padding="5dp"
                        android:text="@string/servings"
                        android:textColor="@color/white"
                        android:textSize="16sp" />

                    <LinearLayout
                        android:layout_width="1dp"
                        android:layout_height="match_parent" >

                        <View style="@style/Divider" />
                    </LinearLayout>

    ..........
    .......
    ......

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:bottom="-3dp"
        android:left="-3dp"
        android:top="-3dp">

        <shape android:shape="rectangle">
            <solid android:color="@color/colorPrimary" />
            <stroke
                android:width="2dp"
                android:color="#1fc78c" />
        </shape>

    </item>

</layer-list>

To make a vertical line, just use a rectangle with width of 1dp:

<shape>
    <size
        android:width="1dp"
        android:height="16dp" />
    <solid
        android:color="#c8cdd2" />
</shape>

Don't use stroke, use solid (which is the "fill" color) to specify the color of the line.


It looks like no one mentioned this option:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@color/white" android:width="1dp"/>
</layer-list>

 <View
        android:layout_width="2dp"
        android:layout_height="40dp"

        android:background="#ffffff"
        android:padding="10dp" />`

참고URL : https://stackoverflow.com/questions/2658772/vertical-line-using-xml-drawable

반응형