2017-02-15 157 views
-1

其實我正在一個佈局中,我必須將CardView放在ImageView上,它只覆蓋ImageView的一半部分,並添加一個TextView(Check Attach Image) 。如何創建此視圖(佈局)

注:

  1. 我嘗試使用的FrameLayout,並與相對佈局... 和我實現它,但我不認爲這是一個正確的做法,因爲我硬編碼大多paramters。

enter image description here

XML代碼:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/toolbar"> 
    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     > 
    <ImageView 
    android:layout_width="match_parent" 
    android:layout_height="170dp" 
    android:layout_gravity="center" 
    android:background="@drawable/background" 
    android:id="@+id/picture"> 
</ImageView> 

    <TextView 
     android:text="TEXT VIEW" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="149dp" 
     android:layout_marginStart="149dp" 
     android:layout_marginTop="24dp" 
     android:id="@+id/textView" /> 
    </FrameLayout> 
</LinearLayout> 

<android.support.v7.widget.CardView 
    android:layout_width="match_parent" 
    android:layout_height="120dp" 
    app:cardBackgroundColor="@color/textViewBackground" 
    card_view:cardUseCompatPadding="true" 
    android:layout_centerHorizontal="true" 
    card_view:cardElevation="15sp" 
    android:layout_marginTop="153dp" /> 
+0

您是否嘗試過這樣做嗎?如果是,那麼發佈的XML代碼 – Vishwa

+0

請檢查我的代碼 –

+0

嗨,兄弟你已經在你的代碼中給出了硬編碼的邊際 –

回答

1

試試這個:

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

<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="200dp" 
    android:background="@android:color/white" 
    android:layout_alignParentTop="true"/> 

<TextView 
    android:layout_width="250dp" 
    android:layout_height="50dp" 
    android:layout_margin="20dp" 
    android:layout_centerHorizontal="true" 
    android:background="@color/colorAccent"/> 

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="200dp" 
    android:layout_marginTop="100dp" 
    card_view:cardCornerRadius="8dp" 
    android:layout_marginLeft="30dp" 
    android:layout_marginRight="30dp" 
    card_view:cardBackgroundColor="@color/colorPrimary"> 


</android.support.v7.widget.CardView> 
</RelativeLayout> 

,如果你不想硬編碼參數,然後參考該鏈接以獲取設備寬度運行時高度

Get screen dimensions in pixels

然後您可以使用LayoutParams動態設置邊距。

+0

其實我不想硬編碼的參數(android:layout_height =「200dp」) –

+0

@DhruvTyagi看到我的編輯 –

+0

oky所以我有疑問, 是任何需要創建多個屏幕尺寸xml? –

0

嗨檢查這個我編輯了自己的代碼

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

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/toolbar"> 
     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      > 
      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="170dp" 
       android:layout_gravity="center" 
       android:background="@color/colorPrimary" 
       android:id="@+id/picture"> 
      </ImageView> 

      <TextView 
       android:text="TEXT VIEW" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="149dp" 
       android:layout_marginStart="149dp" 
       android:background="@color/colorBlueColor" 
       android:layout_marginTop="24dp" 
       android:id="@+id/textView" /> 
     </FrameLayout> 

     <android.support.v7.widget.CardView 
      android:layout_width="match_parent" 
      android:layout_height="120dp" 
      app:cardBackgroundColor="@color/textViewBackground" 
      card_view:cardUseCompatPadding="true" 
      android:layout_centerHorizontal="true" 
      android:layout_marginLeft="100dp" 
      card_view:cardElevation="15sp" 
      android:layout_marginTop="100dp" /> 

    </RelativeLayout> 



</LinearLayout> 
0

嘗試這種方式,將工作

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:arc="http://schemas.android.com/apk/res-auto" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" > 

    <ImageView 
     android:id="@+id/imageView1s" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:scaleType="fitXY" 
     android:src="@drawable/done" /> 

    <android.support.v7.widget.CardView 
     android:layout_width="130dp" 
     android:layout_height="130dp" 
     android:layout_marginTop="-65dp" 
     android:layout_below="@+id/imageView1s" 
     android:layout_centerHorizontal="true" 
     android:contentDescription="@string/app_name" 
     android:scaleType="center" 
     /> 

</RelativeLayout>