2012-06-22 7 views
0

ImageView을 화면 상단에 맞추려면 이미지의 왼쪽 하단에 TextView이 있어야합니다. 나는 어려움을 겪고있다. 나는 이것이 (그래픽 디자이너에서 잘 보이는)처럼이사용자 정의보기에서 오버레이로 이미지의 기준선에 텍스트 정렬

여기

enter image description here

는 응용 프로그램의 모습입니다보고 싶어요. 이미지와 화면 상단 사이에는 큰 공간이 있습니다.

enter image description here

여기 여기 XML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <com.package.blah.ImageWithOverlay 
     android:id="@+id/imageWithOverlay1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
    </com.package.blah.ImageWithOverlay> 

</RelativeLayout> 

인을 생성하기위한 코드이다.

public class ImageWithOverlay extends RelativeLayout { 
    ImageView image; 
    TextView text; 
    int imageHeight; 

    public ImageWithOverlay(Context context) { 
     super(context); 
     setupDisplay(context); 
    } 

    public ImageWithOverlay(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setupDisplay(context); 
    } 

    public ImageWithOverlay(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     setupDisplay(context); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
     //super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     super.onMeasure(widthMeasureSpec, imageHeight); 
    } 

    private void setupDisplay(Context context) { 
     BitmapDrawable bd = (BitmapDrawable)this.getResources().getDrawable(R.drawable.flowers480); 
     imageHeight = bd.getBitmap().getHeight(); 

     image = new ImageView(context); 
     image.setImageDrawable(bd); 
     LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
     lp.addRule(ALIGN_PARENT_TOP); 
     image.setLayoutParams(lp); 
     this.addView(image); 

     text = new TextView(context); 
     text.setText("testing"); 
     lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
     lp.addRule(ALIGN_PARENT_BOTTOM); 
     text.setLayoutParams(lp); 
     this.addView(text); 
    } 
} 

도움이 필요하시면 언제든지 문의 해 주시기 바랍니다.

+1

lp.addRule (RelativeLayout.ALIGN_BASELINE)을 사용하고 getBaseline 메서드를 재정 의하여 사용자 정의 위치에 배치 할 수 있습니다.이 블로그에는 원반이 있습니다. http://sudarnimalan.blogspot.sg/2011/09/android-aligning-custom -views-at-base.html –

답변

1

ImageView의 크기가 맞지만 이미지의 크기가 조정되고 뷰 중앙에 배치되어 있다고 생각합니다.

추가, 수정하려면 :

image.setAdjustViewBounds(true); 

이 같은 문제를 확인하려면, 당신은 시도 할 수있게된다

image.setBackgroundColor(0x6600ffff); 

당신은 이미지의 경계를 참조하십시오.

+0

완벽합니다. 보시고 배경 색상을 알려 주셔서 감사합니다. TextView가 맨 아래에 있다는 것이 맞았습니다. – Kirk

관련 문제