2015-07-21 174 views
0

我正在处理我的应用程序中的XML布局,并且我想要做的是使ImageView上有一段文字(我想要一个TextView在上面)。无论如何,少些话和更多的例子。我想是这样的:Android XML布局 - 带有TextView的图片

enter image description here

我想知道什么是使(用不透明带,最好用文字将伸展或因名称的长度缩水的最好办法尽管我认为我可能会自己想出延伸)。

+1

你可以使用绝对布局或框架布局和位置和大小的一切都是父母。 –

回答

2

您可以创建一个ImageViewTextView作为两个孩子

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <ImageView 
     android:id="@+id/myImageView" 
     android:layout_width="150dp" 
     android:layout_height="75dp" 
     android:src="@drawable/myImage" /> 

    <TextView 
     android:id="@+id/myTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/myImageView" 
     android:layout_alignParentLeft="true" 
     android:layout_alignRight="@+id/myImageView" 
     android:background="@drawable/myBackground" 
     android:gravity="center" /> 
</RelativeLayout> 
+0

谢谢你的回答:)我对这些'RalativeLayout'非常不满。我的drawable中会有什么?一条不透明的油漆条?也许有一种方法不使用drawable,而是自己在程序中绘制它?如果我需要动态尺寸的条纹 – Vendetta8247

+0

您也可以使用'@android:color /'或好的十六进制编码的ARGB作为背景。例如:'@android:color/red'或'#BBFFFFFF' – ChrisStillwell

1

建立在什么克里斯·史迪威说RelativeLayout,alpha属性设置为小于1:

<RelativeLayout 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" > 

<ImageView 
    android:id="@+id/myImageView" 
    android:layout_width="150dp" 
    android:layout_height="75dp" 
    android:src="@drawable/myImage" /> 

<TextView 
    android:id="@+id/myTextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/myImageView" 
    android:layout_alignParentLeft="true" 
    android:layout_alignRight="@+id/myImageView" 
    android:background="@drawable/myBackground" 
    android:gravity="center" /> 
</RelativeLayout> 

然后在你的onCreate()

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.yourlayout); 
    ImageView imageView = (ImageView)findViewById(R.id.myImageView); 
    TextView textView = (TextView)findViewById(R.id.myTextView); 
    textView.setText("Your Text"); 
    textView.setAlpha(.6f); 
} 
+0

感谢您使用代码所做的更改和补充。我已经提出了你的答案,但我会将ChrisStillwell的答案标记为正确的,因为他是第一个。尽管我仍然非常感谢你 – Vendetta8247