2017-01-14 18 views
1

相対レイアウト内にイメージとテキストビューがあります。画像とテキストビューは画面幅の50%を占めます。どのようにイメージとテキストビューの中央に配置できますか?私は多くの方法を試してみましたし、私は私が間違ってやっているのか分からない...ここで相対イメージレイアウトの中心ImageView

は私のコードです:

 <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="100"> 
     <RelativeLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="50" 
      android:layout_centerHorizontal="true"> 

      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/textViewEventImage" 
       android:adjustViewBounds="true" /> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="true" 
       android:background="@color/eventNameBackground" 
       android:id="@+id/textViewEventName" 
       android:textColor="@android:color/white" 
       android:textSize="15sp"/> 
     </RelativeLayout> 
    </LinearLayout> 

enter image description here

+0

あなたのイメージが画面の中央になりたいでしょう。このコードを試してみてください助けることができますか? –

答えて

2

が、それはスーツあなたの要件が

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

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.5" 
     android:background="#ff0000" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="180dp" 
     android:layout_height="120dp" 
     android:layout_centerInParent="true" 
     android:gravity="center_vertical" 
     android:src="@drawable/americanexpress" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_below="@+id/imageView1" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="16dp" 
     android:text="Your product" /> 
</RelativeLayout> 

<View 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.5" 
    android:background="#00ff00" /> 

</LinearLayout> 
+0

コメントありがとう!:) –

0

あなたが中心にImageViewのとのTextViewをしたい場合それを使ってください。

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

    <ImageView 
     android:id="@+id/textViewEventImage" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:src="@mipmap/ic_launcher" /> 

    <TextView 
     android:id="@+id/textViewEventName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textViewEventImage" 
     android:layout_centerInParent="true" 
     android:text="There is Your Text" 
     android:textSize="15sp" /> 
</RelativeLayout> 
1

centerinparent ="true" 

textviewcenterinparent="true"

としてimageviewを入れて、この

のように試してみて、

android: layout_below="@+id/imageview id" 
として textview に一つの属性を追加
+0

それは動作しません。画像ビューはまだ画面の左側にあり、テキストビューは画像ビューの途中にあります。 –

+0

投稿に画像を追加しました –

0

が、これは

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true"> 
    <ImageView 
     android:layout_width="xxxdp" 
     android:layout_height="xxxdp" 
     android:src="@drawable/someimage" 
     /> 
    <TextView 
     android:layout_width="xxxdp" 
     android:layout_height="xxxdp" 
     android:text="fweqf"/> 
    </LinearLayout> 

</RelativeLayout> 
+0

私は相対レイアウトを使用しなければなりません画像ビューにテキストを表示するには... –

+0

私のansは、あなたがLinearLayoutであるビューを含むRelativeLayoutを使用している参照した画像として結果を生成します。とにかく「イメージビュー上のテキスト」とはどういう意味ですか?あなたのイメージをあなたのテキストの背景のように見せたいですか?私は、あなたがしたいことをもう少し知りたいです。 – Simo

関連する問題