2016-07-04 5 views
1

FrameLayout의 TextView 뒤에 이미지를 넣고 싶습니다. TextLight 내부의 높이를 조정하고 이미지 크기의 영향을받지 않기 위해서는 FrameLayout이 필요합니다. 이미지는 FrameLayout에 의해 잘린 원래 크기의 텍스트 뒤에 있어야합니다.wrap_content 높이를 사용하여 FrameLayout의 이미지 뷰 자르기

이것은 내가 시도한 것입니다.

<FrameLayout 
android:layout_width="0dp" 
android:layout_height="wrap_content" 
android:layout_weight="1"> 
<ImageView 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:scaleType="centerCrop" 
android:layout_gravity="right|center_vertical" 
android:gravity="center" 
android:alpha="0.1" 
android:id="@+id/img_logo"/> 
<TextView 
android:textAppearance="?android:attr/textAppearanceMedium" 
android:textAllCaps="true" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_marginLeft="10dp" 
android:layout_marginRight="10dp" 
android:layout_marginTop="10dp" 
android:layout_marginBottom="10dp" 
android:gravity="left|center_vertical" 
android:id="@+id/text_long" /> 
</FrameLayout> 

FrameLayout의 높이가 이미지 크기의 영향을 받는다는 점을 제외하면 거의 원하는 것입니다. 텍스트로만 변경해야합니다.

+0

이것은 해결하기 까다로운 문제입니다! 내 초기 반응은 ImageView의 높이가 wrap_content이고 프레임 뷰의 크기를 TextView의 높이에 맞게 동적으로 조정하는 동안 FrameaLayout의 높이를 고정시켜야합니다. 이 솔루션은 좀 더 우아한 접근 방법입니다 (코드는 없지만, 코드를보고 싶으면 답을 얻을 수 있습니다) : http://stackoverflow.com/a/21581103/6526330 –

+0

@ Dr.Nitpick 시도해 보았습니다. 그것은 제대로 작동하지 않았습니다. 당신이 그것을 작동하게 만들 수 있다면 기꺼이 당신의 대답을 받아 들일 것입니다. –

+0

물론 이죠, 오늘 밤에 시험해보고 알려주세요. –

답변

2

사실 나는 레이아웃을 재정의하거나 자바 코드를 작성하지 않고이 작업을 수행 할 수 있다고 생각합니다. 대신 RelativeLayout에있는 모든 것을 래핑 할 수 있으며 FrameLayout 뒤에 TextView을 이동하고 FrameLayout의 위/아래를 textView에 맞춰 정렬하도록 설정할 수 있습니다. 나는 조금 지저분하다는 것을 인정한다; 내가 사용자 정의보기로 만들거나 사용자가 포함하는 다른 xml 파일에 넣는 것이 좋습니다.

편집 : 사실, 더 이상 FrameLayout이 필요하지 않으며 대신 ImageView에서 android : layout_alignTop 및 android : layout_alignBottom 속성을 설정할 수 있습니다. 모양은 다음과 같습니다.

<RelativeLayout 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"> 
    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_alignTop="@+id/text_long" 
     android:layout_alignBottom="@id/text_long" 
     android:scaleType="centerCrop" 
     android:layout_gravity="right|center_vertical" 
     android:gravity="center" 
     android:alpha="0.1" 
     android:id="@+id/img_logo"/> 
    <TextView 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textAllCaps="true" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="10dp" 
     android:layout_marginBottom="10dp" 
     android:gravity="left|center_vertical" 
     android:id="@id/text_long" /> 
</RelativeLayout> 
관련 문제