简体   繁体   中英

Android layout creating two rows with one column combined for the picture

I'm trying to create a layout like this. First off, is GridLayout the best way to go about this? I tried many different layouts but each had their own issue. Below is the code I tried. Any help is appreciated. Thanks in advance.

尝试布局

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

            <ImageButton
        android:src="@drawable/avatar_male_update"
        android:scaleType="fitCenter"
        android:adjustViewBounds="true"
        android:layout_height="100dp"
        android:layout_width="100dp"
        android:layout_row="0"
        android:layout_column="0"
        android:id="@+id/imageButtonMale" />

                <Button
        android:text="Refresh"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:background="@drawable/mybutton"

        android:layout_row="0"
        android:layout_column="1" 
        android:id="@+id/btnCSRefresh" />

                <Button
        android:text="Change Avatar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/mybutton"
        android:layout_row="0"
        android:layout_column="2"
        android:id="@+id/btnChangeAvatar" />

            <LinearLayout
    android:orientation="vertical"
                android:layout_row="1"
        android:layout_column="0"
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_height="50dp"

    android:id="@+id/linearLayoutButtons">

         <TextView
    android:text="Welcome "
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:id="@+id/txtViewHeader"
    android:background="#FE6A00" />
<ListView
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:id="@+id/lstViewSummary" />

    </LinearLayout>

what you really want is not very clear to me as you are using a listview in your xml, so plz post your problem with more clarity.

try this for your layout:

  <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:minHeight="100dp" android:minWidth="100dp" android:src="@drawable/ic_launcher" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/button1" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/button1" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button2" android:layout_toRightOf="@+id/button3" /> </RelativeLayout> </LinearLayout> 

I should downvote your question because your xml does not match your picture!

But if you want what you've drawn in your picture:

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">

  <ImageView 
    android:align_parentLeft="true"
    android:center_in_vertical="true"
    android:id="@+id/imageButtonMale"
    android:layout_height="100dp"
    android:layout_width="100dp" />

    <View 
      android:id="@+id/spacer1"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:align_toRightOf="@id/imageButtonMale"
      android:center_vertical="true" />

    <Button
      android:text="Refresh"
      android:id="@+id/btnCSRefresh"
      android:layout_above="@id/spacer1"
      android:align_toRightOf="@id/imageButtonMale"
      android:layout_width="100dp"
      android:layout_height="100dp" />

    <Button
      android:text="Change Avatar"
      android:id="@+id/btnChangeAvatar"
      android:layout_above="@id/spacer1"
      android:align_toRightOf="@id/btnCSRefresh"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />

    <Button
      android:text="Row2Col1"
      android:id="@+id/button_below_refresh_button"
      android:layout_below="@id/spacer1"
      android:align_toRightOf="@id/imageButtonMale"
      android:layout_width="100dp"
      android:layout_height="100dp" />

    <Button
      android:text="Row2Col2"
      android:id="@+id/button_below_change_avatar_button"
      android:layout_below="@id/spacer1"
      android:align_toRightOf="@id/button_below_in_col1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
</RelativeLayout>

Some points:

This will wrap around, meaning if you wanted this to expand to screen width, you would change relative layout parent width to match_parent . Now, depending if you want the buttons to be of equal width, you can could change their widths to match_parent but then you MUST change android:align_parentRight to both buttons in column 2, and add android:align_toLeftOf="@id/button_in_col2" to the buttons in column one (that is, buttons in columns one are to the right of the imageview and to the right of the buttons in column two; the buttons in column to align to their parent right; both buttons, matching parent with the aforementioned alignments will take up equal space).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM