2011-10-09 2 views
3

이 여기에서 알 수 있듯이 내가 레이아웃의 중심과 하단에 FrameLayout이의 이미지를 통해 텍스트 뷰를 구현하기 위해 노력하고 있습니다 :오버레이 텍스트 - 안드로이드

http://developer.android.com/resources/articles/layout-tricks-merge.html

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

<ImageView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 

    android:scaleType="center" 
    android:src="@drawable/golden_gate" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="20dip" 
    android:layout_gravity="center_horizontal|bottom" 

    android:padding="12dip" 

    android:background="#AA000000" 
    android:textColor="#ffffffff" 

    android:text="Golden Gate" /> 

</FrameLayout> 

이 프로그래밍 방식으로 구현하는 데 노력하고 있지만 행운을 함께 .. 나는 항상 왼쪽 상단 모서리에있는 textview 얻을. 아무도 도움이? 여기 내 코드입니다 :

FrameLayout frameLay = new FrameLayout(MainScreen.this);        
LayoutParams layoutParamsFrame = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 

frameLay.setLayoutParams(layoutParamsFrame); 

LinearLayout.LayoutParams layoutParamsImage= new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 

ImageView imageView= new ImageView(MainScreen.this); 
imageView.setImageResource(R.drawable.movie); 
imageView.setLayoutParams(layoutParamsImage); 

TextView theText=new TextView(MainScreen.this); 
theText.setText("GOLDEN Gate"); 
theText.setTextColor(Color.WHITE);       
theText.setTypeface(Typeface.DEFAULT_BOLD); 

LayoutParams layoutParamsText= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

theText.setLayoutParams(layoutParamsText); 

theText.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM); 

frameLay.addView(theText); 
frameLay.addView(imageView); 

답변

7

당신이 IAS를해야 할 모든 당신이 텍스트 뷰에 중력을 설정하면 텍스트 뷰가

LayoutParams layoutParamsText= new LayoutParams(LayoutParams.FILL__PARENT, 
     LayoutParams.FILL_PARENT); 

같은 부모를 채울 수 있도록, 당신이 위치로 텍스트 뷰를 말하는 것을 의미 자녀를 배치하십시오. 그러나 텍스트 뷰에는 텍스트 크기 만 있기 때문에 중력에는 차이가 없습니다. 그러니 부모님을 채우기 위해 textview를 만드십시오.

하지만 RelativeLayout이 FrameLayout보다 훨씬 적합하다고 생각합니다. RelativeLayout의를 사용하여이 그것이 FrameLayout이의 대신

RelativeLayout rLayout = new RelativeLayout(this); 
LayoutParams rlParams = new LayoutParams(LayoutParams.FILL_PARENT 
     ,LayoutParams.FILL_PARENT); 
rLayout.setLayoutParams(rlParams); 

ImageView image= new ImageView(this); 
image.setImageResource(R.drawable.icon);  
image.setLayoutParams(rlParams); 

RelativeLayout.LayoutParams tParams = new RelativeLayout.LayoutParams 
     (LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
tParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); 
tParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); 
TextView text=new TextView(this); 
text.setText("GOLDEN Gate"); 
text.setTextColor(Color.WHITE);        
text.setTypeface(Typeface.DEFAULT_BOLD); 
text.setLayoutParams(tParams); 

rLayout.addView(image); 
rLayout.addView(text); 
setContentView(rLayout); 
+0

작동합니다! 대단히 감사합니다! – andreasv

+0

@ 브레멘 감사합니다. 백만 달러 –

1

를 어떻게 보일지, 당신은 컨테이너로 RelativeLayout의를 사용하여 시도 할 수 있습니다. 이렇게하면 원하는 위치에 오버레이 텍스트를 배치 할 수 있습니다. 당신을 위해 일해야하는 것은 RelativeLayout의 내 텍스트 뷰에 다음을 지정하는 것입니다 :

android:layout_alignParentBottom="true" and android:layout_centerInParent="true" 

할 수 있습니다 다음 미세 조정 여백 배치.

관련 문제