簡體   English   中英

Android在RelativeLayout中頂部和中央對齊圖像

[英]Android align image in top and center in RelativeLayout

我正在嘗試將圖像對齊到相對布局的頂部,然后將其水平居中。 這是我正在使用的代碼:

<ImageView  
        android:id="@+id/collection_image_background"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"  />

,但是此代碼無法正常工作。 我的圖像居中,但未在“相對布局”的頂部對齊。 我嘗試使用android:scaleType="fitStart" ,但實際上是將imag左對齊,並將其頂部對齊。

那么,有什么想法可以根據需要正確對齊圖像嗎?

PS我忘了提起我將圖像設置為ImageView如下所示:

    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inTempStorage = new byte[8*1024];

    ops = BitmapFactory.decodeStream(cis,null,o2);
    ImageView view = (ImageView) findViewById(R.id.collection_image_background);
    view.setImageBitmap(ops);

代碼似乎很完美,除了您已分配的內容區域fill_parent占據了整個視圖並將顯示為中心,因此只需通過wrap_content對其進行修改

  <ImageView  
            android:id="@+id/collection_image_background"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"  />

暫無
暫無

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

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