java - How to center an FrameLayout in a RelativeLayout -


i've problem layout , great, if android experts check , me :)

i have fragmenta contains fragmentb.

  • fragmenta contains 3 framelayouts:

      1. framelayout: should @ top
      1. framelayout: should below first an contains fragmentb (id= frame2)
      1. framelayout: should @ bottom
  • fragmentb contains 1 framelayout:

    • i'm adding programmatically view (e.g. image) framelayout in way:

framelayout.addview(new imageclass(id); 

the problem is, image not centered horizontally in middle.

this layout file of first fragmenta:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:gravity="center_horizontal"     android:layout_width="match_parent"     android:layout_height="match_parent">      <framelayout         android:id="@+id/frame1"         android:layout_width="match_parent"         android:layout_height="wrap_content" />      <framelayout         android:id="@+id/frame2"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_below="@+id/frame1" />      <framelayout         android:id="@+id/frame3"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_alignparentbottom="true" />  </relativelayout> 

and layout file of fragmentb (included in fragmenta -> framelayout 2 id frame2)

<framelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/imageclass"     android:background="#0000ff"     android:layout_width="wrap_content"     android:layout_height="wrap_content"> </framelayout> 

and image insert in view canvas code:

bitmap image= bitmapfactory.decoderesource(getresources(), id); canvas.drawbitmap(image, 0, 0, null); 

i thought android:gravity="center_horizontal" in relativelayout , android:layout_width="wrap_content" in both framelayouts of fragmenta und fragmentb, image should centered. on left side.

there 2 screenshots how looks (first) , how should (second):

how looks

how should look

sorry links, cannot post pictures (not enought reputation)

relativelayouts handle gravity differently may expect (link):

note since relativelayout considers positioning of each child relative 1 significant, setting gravity affect positioning of children single unit within parent. happens after children have been relatively positioned.

a quick fix change frame2 framelayout relativelayout, add match_parent width , put center gravity on that.

    <relativelayout         android:id="@+id/frame2"         android:gravity="center_horizontal"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_below="@+id/frame1"/> 

there number of other approaches take depending on requirements view. it's better avoid nesting relativelayouts in case may necessary.

possible duplicate here.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -