2017-02-08 3 views
1

아래 보이는 틈을 제거하려고합니다. View은 서로 마주보고 있으므로 drawable에있는 것 같습니다. strokewidth과 관련되어 있음을 알 수 있습니다. 어떻게이 효과를 없앨 수 있습니까?Drawable Shape 라인의 간격을 제거하십시오.

이것은 MVCE 일 뿐이며 실제 사용 사례에서는 View보다 작은 줄을 사용해야 함을 유의하십시오.

의심의 여지를 없애기 위해 나는 drawable xml로 해결하는 답변 만 수락합니다. 나는 레이아웃이 주위에 일하는 것을 원하지 않는다. 레이아웃은 단지 문제를 드러내는 것이다.

Gap

드로어 블/사진 line.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="line"> 
    <stroke 
     android:width="5dp" 
     android:color="#ff0000"/> 
</shape> 

레이아웃/example.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 
    <View 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@drawable/line"> 
    </View> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@drawable/line"> 
    </View> 
</LinearLayout> 
+0

내 생각에 패딩 = 0 –

답변

0

두 솔루션 :

1) 쉬 변경 원숭이는 레이아웃

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="5dp" 
     android:layout_weight="1" 
     android:background="@drawable/line"> 
    </View> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="5dp" 
     android:layout_weight="1" 
     android:background="@drawable/line"> 
    </View> 
</LinearLayout> 

2) 다른 접근 방식을 가지고, 그리고 라인

<View 
    android:layout_width="match_parent" 
    android:layout_height="5dp" 
    android:background="@android:color/holo_red_light"/> 

편집에 대한 XML 묘화를 만들 수 없습니다에

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="rectangle"> 

    <solid android:color="#ff0000"/> 
    <size android:width="100dp" android:height="1dp"/> 

</shape> 

그런 다음 사각형에 XML 선언

너는 뷰는 선의 사이즈보다 커집니다. 이 효과를 얻기 위해 이미지보기와 사각형 XML 모양과 함께 src 속성을 사용할 수 이러한 격차는 그리기 모양 라인에서 오는가

<ImageView 
    android:layout_width="0dp" 
    android:layout_height="50dp" 
    android:layout_weight="1" 
    android:src="@drawable/line"> 
</ImageView> 

<ImageView 
    android:layout_width="0dp" 
    android:layout_height="50dp" 
    android:layout_weight="1" 
    android:src="@drawable/line"> 
</ImageView> 
+0

내 예제는 [MCVE]입니다. 실제로 사용하는 경우에는 줄이 필요합니다. – weston

+0

네, 그 정도면 충분합니다! 나는 모양을 가질 것이다 – Charlie

+0

그것은 좋은 fallback이고 아마 내가 어디에서나 얻을 수 없다면 나는 무엇을 할 것인가. – weston

1

? 모양 드로어 블을 만들기위한 책임

클래스는

GradientDrawable.java이의 소스 코드를 살펴 보자입니다. draw(Canvas canvas) 방법에서

은 :

switch (st.mShape) { 
case LINE: { 
     RectF r = mRect; 
     float y = r.centerY(); 
     if (haveStroke) { 
      canvas.drawLine(r.left, y, r.right, y, mStrokePaint); 
     } 
     break; 
    } 
} 

행은 이제 mRect가 수정되는 경우 보자 mRect.right

mRect.left에서 그려집니다.ensureValidRect() 방법에

:

Rect bounds = getBounds(); 
float inset = 0; 
if (mStrokePaint != null) { 
    inset = mStrokePaint.getStrokeWidth() * 0.5f; 
} 
final GradientState st = mGradientState; 
mRect.set(bounds.left + inset, bounds.top + inset, bounds.right - inset, bounds.bottom - inset); 

가 하프 스트로크 폭과 동일한 inset을 볼 수있는 바와 같이, 부가된다.

귀하의 틈이 생기는 곳입니다.

어떻게하면이 효과를 없앨 수 있습니까?

  1. 당신은

    드로어 블/사진 line_inset.xml (스트로크 폭의 절반을해야합니다) 자신의 부정적인 삽입을 추가 할 수 있습니다

    <?xml version="1.0" encoding="utf-8"?> 
    <inset xmlns:android="http://schemas.android.com/apk/res/android" 
        android:drawable="@drawable/line" 
        android:insetLeft="-2.5dp" 
        android:insetRight="-2.5dp"/> 
    
  2. 9 패치를 사용하는 것을 고려 드로어 블

red line 9-patch

줄 위쪽과 왼쪽은 스트레치 영역을 정의합니다. 내가 빈 공간 만 늘리고 있다는 것을 알 수 있습니다. 오른쪽과 아래의 줄은 내용 영역을 정의합니다.이 경우에는 모든 공백이 정의됩니다.

+0

제거 방법에 대한 제안이 있습니까? – weston

+0

+1 스트로크 너비와의 관계를 확인해 주셔서 감사합니다. 그것은 하나의 해결책으로 나를 이끌었지만, 다른 제안을하기 위해 약간의 노력을 기울일 것입니다. – weston

+0

'line' 형태로 그것을 제거하는 방법을 볼 수 없습니다. 9 패치 드로어 블을 대신 사용해 보셨습니까? –

0

레이어 목록 및 항목을 랩핑하면 leftright까지 확장 할 수 있습니다. 나는 그들이 필요 이상으로 나아가는 것에 대해 걱정할 필요가 없습니다. 여전히 가장자리로자를 것입니다. 이것은 효과를 없앤다.

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:left="-5dp" 
     android:right="-5dp"> <!-- negative stroke width --> 
     <shape android:shape="line"> 
      <stroke 
       android:width="5dp" 
       android:color="#ff0000"/> 
     </shape> 
    </item> 
</layer-list>