簡體   English   中英

如何將textview放置在中間相對布局中?

[英]How to place textview in the middle relative layout?

我只是想知道要在文本視圖的中間放置文本視圖時需要使用哪種相對布局參數。 如果這不可能,那么我該怎么辦。

您可以使用RelativeLayout,將圖像設置為背景,並使用Center_In_parent作為textview。

android:centerInParent="true"

要么

android:layout_centerHorizontal="true"

要么

android:layout_centerVertical="true"

您可以根據需要使用任何一種

ImageView不是視圖組,因此無法完全做到這一點。

您需要做的是將ImageView和textview都放在框架布局中。 然后使用android:centerInParent =“ true”將textview居中。

RelativeLayout relativeLayout;//set the relative layout
text=new TextView(context);
ImageView image=new ImageView(context); 

RelativeLayout.LayoutParams imageParams = new      
RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams textParams = new     
RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
textParams.addRule(RelativeLayout.CENTER_IN_PARENT);
imageParams.addRule(RelativeLayout.CENTER_IN_PARENT);
relativeLayout.addView(image,imageParams);
relativeLayout.addView(text,textParams);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM