正确设置图像在中心

时间:2017-04-20 09:24:45

标签: android imageview android-linearlayout

我将屏幕分为4个水平和2个垂直部分。我使用线性布局显示ImageView和TextView。我无法将imageView准确设置在线性布局的中心。

home_layout

<?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">


    <include layout="@layout/appbar_layout" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:background="#156C7F"
        android:orientation="horizontal">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="125dp"
            android:text="Branch Office : Dhankuta"
            android:textColor="#FFFFFF"
            android:textSize="10sp" />

        <View
            android:layout_width="1dp"
            android:layout_height="15dp"
            android:layout_gravity="center"
            android:layout_marginLeft="7dp"
            android:background="#FFFFFF" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="7dp"
            android:text="Tuesday - 4 April, 2017"
            android:textColor="#FFFFFF"
            android:textSize="10sp" />


    </LinearLayout>

    <!--  Main Linear Layout -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#DFDFDF"
        android:orientation="vertical">

        <!--   first Frame -->


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">

            <!-- first row first column -->

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="5dp"
                android:layout_weight="1"
                android:background="#ffffff"
                android:gravity="center"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_gravity="center"
                    android:src="@mipmap/saving"></ImageView>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Saving Collection"
                    android:textColor="#494949"
                    android:textSize="13sp"
                    android:textStyle="bold" />


            </LinearLayout>


            <!-- first row second column -->

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="5dp"
                android:layout_weight="1"
                android:background="#ffffff"
                android:gravity="center"
                android:orientation="vertical">

                <ImageView  **i have made changes here**
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_gravity="center"
                    android:src="@mipmap/loancollection"></ImageView>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Loan Collection"
                    android:textColor="#494949"
                    android:textSize="13sp"
                    android:textStyle="bold" />


            </LinearLayout>


        </LinearLayout>


        <!-- 2nd frame  -->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">

            <!-- Second row first column -->

            <LinearLayout       **i have made changes here**
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_weight="1"
                android:background="#ffffff"
                android:gravity="center"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_gravity="center"

                    android:src="@mipmap/dailysaving"></ImageView>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Daily Saving"
                    android:textColor="#494949"
                    android:textSize="13sp"
                    android:textStyle="bold"
               />


            </LinearLayout>


            <!-- Second row Second column -->

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"

                android:layout_weight="1"
                android:background="#ffffff"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_gravity="top|center"
                    android:layout_marginTop="10dp"
                    android:src="@mipmap/memberenrollment"></ImageView>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="5dp"
                    android:text="Member Enrollment"
                    android:textColor="#494949"
                    android:textSize="13sp"
                    android:textStyle="bold" />


            </LinearLayout>


        </LinearLayout>


        <!-- 3rd frame  -->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">

            <!-- Third row First column -->


            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"

                android:layout_weight="1"
                android:background="#ffffff"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_gravity="top|center"
                    android:layout_marginTop="10dp"
                    android:src="@mipmap/cashincashout"></ImageView>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="5dp"
                    android:text="Cash In-Out "
                    android:textColor="#494949"
                    android:textSize="13sp"
                    android:textStyle="bold" />


            </LinearLayout>
            <!-- Third row Second column -->
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"

                android:layout_weight="1"
                android:background="#ffffff"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_gravity="top|center"
                    android:layout_marginTop="10dp"
                    android:src="@mipmap/collectorledger"></ImageView>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="5dp"
                    android:text="Collector Ledger "
                    android:textColor="#494949"
                    android:textSize="13sp"
                    android:textStyle="bold" />


            </LinearLayout>


        </LinearLayout>


        <!-- 4th frame  -->


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">


            <!-- Fourth row First column -->


            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"

                android:layout_weight="1"
                android:background="#ffffff"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_gravity="top|center"
                    android:layout_marginTop="10dp"
                    android:src="@mipmap/summary"></ImageView>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="5dp"
                    android:text="Summary"
                    android:textColor="#494949"
                    android:textSize="13sp"
                    android:textStyle="bold" />


            </LinearLayout>

            <!-- Fourth row Second column -->


            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"

                android:layout_weight="1"
                android:background="#ffffff"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_gravity="top|center"
                    android:layout_marginTop="10dp"
                    android:src="@mipmap/officesync"></ImageView>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="5dp"
                    android:text="Office Sync"
                    android:textColor="#494949"
                    android:textSize="13sp"
                    android:textStyle="bold" />


            </LinearLayout>


        </LinearLayout>


        <!-- 4th frame end  -->


    </LinearLayout>   <!-- main frame end Layout -->


</LinearLayout>

我对第一行第一列和第一行第二列进行了更改

  

**我希望imageView完全位于线性布局的中心,然后文本将调整其余部分。**如何解决此问题?

5 个答案:

答案 0 :(得分:1)

enter image description here

<强> UseBelowCode

<?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">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:background="#156C7F"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="125dp"
        android:text="Branch Office : Dhankuta"
        android:textColor="#FFFFFF"
        android:textSize="10sp" />

    <View
        android:layout_width="1dp"
        android:layout_height="15dp"
        android:layout_gravity="center"
        android:layout_marginLeft="7dp"
        android:background="#FFFFFF" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="7dp"
        android:text="Tuesday - 4 April, 2017"
        android:textColor="#FFFFFF"
        android:textSize="10sp" />
</LinearLayout>

<!--  Main Linear Layout -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DFDFDF"
    android:orientation="vertical">

    <!--   first Frame -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal">

        <!-- first row first column -->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:layout_weight="1"
            android:background="#ffffff"
            android:orientation="vertical">

            <ImageView
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:src="@drawable/conference_room"
                android:layout_centerInParent="true"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Saving Collection"
                android:textColor="#494949"
                android:textSize="13sp"
                android:textStyle="bold" 
                android:gravity="center"
                android:layout_alignParentBottom="true"/>
        </RelativeLayout>

        <!-- first row second column -->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_weight="1"
            android:background="#ffffff"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:src="@drawable/conference_room"
                android:layout_centerInParent="true"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Loan Collection"
                android:textColor="#494949"
                android:textSize="13sp"
                android:textStyle="bold"
                android:gravity="center"
                android:layout_alignParentBottom="true"/>
        </RelativeLayout>
    </LinearLayout>


    <!-- 2nd frame  -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal">

        <!-- Second row first column -->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:background="#ffffff"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:src="@drawable/conference_room"
                android:layout_centerInParent="true"/>


            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Daily Saving"
                android:textColor="#494949"
                android:textSize="13sp"
                android:textStyle="bold"
                android:gravity="center"
                android:layout_alignParentBottom="true"/>
        </RelativeLayout>

        <!-- Second row Second column -->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:background="#ffffff"
            android:orientation="vertical">

            <ImageView
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:src="@drawable/conference_room"
                android:layout_centerInParent="true"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="5dp"
                android:text="Member Enrollment"
                android:textColor="#494949"
                android:textSize="13sp"
                android:textStyle="bold"
                android:gravity="center"
                android:layout_alignParentBottom="true"/>
            </RelativeLayout>
        </LinearLayout>

    <!-- 3rd frame  -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal">

        <!-- Third row First column -->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:background="#ffffff"
            android:orientation="vertical">

            <ImageView
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:src="@drawable/conference_room"
                android:layout_centerInParent="true"/>


            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="5dp"
                android:text="Cash In-Out "
                android:textColor="#494949"
                android:textSize="13sp"
                android:textStyle="bold"
                android:gravity="center"
                android:layout_alignParentBottom="true"/>

        </RelativeLayout>

        <!-- Third row Second column -->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:background="#ffffff"
            android:orientation="vertical">

            <ImageView
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:src="@drawable/conference_room"
                android:layout_centerInParent="true"/>


            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="5dp"
                android:text="Collector Ledger "
                android:textColor="#494949"
                android:textSize="13sp"
                android:textStyle="bold"
                android:gravity="center"
                android:layout_alignParentBottom="true"/>
            </RelativeLayout>
        </LinearLayout>

        <!-- 4th frame  -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">

            <!-- Fourth row First column -->
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_weight="1"
                android:background="#ffffff"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:src="@drawable/conference_room"
                    android:layout_centerInParent="true"/>


                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="5dp"
                    android:text="Summary"
                    android:textColor="#494949"
                    android:textSize="13sp"
                    android:textStyle="bold"
                    android:gravity="center"
                    android:layout_alignParentBottom="true"/>
            </RelativeLayout>

            <!-- Fourth row Second column -->
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_weight="1"
                android:background="#ffffff"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:src="@drawable/conference_room"
                    android:layout_centerInParent="true"/>


                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="5dp"
                    android:text="Office Sync"
                    android:textColor="#494949"
                    android:textSize="13sp"
                    android:textStyle="bold"
                    android:gravity="center"
                    android:layout_alignParentBottom="true"/>
            </RelativeLayout>
        </LinearLayout>
        <!-- 4th frame end  -->
</LinearLayout>   <!-- main frame end Layout -->

答案 1 :(得分:1)

LinearLayout而不是RelativeLayout

<!-- first row first column -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:layout_weight="1"
            android:background="#ffffff"
            android:orientation="vertical"
            android:weightSum="2"
            android:gravity="center">

            <ImageView
                android:id="@+id/sjkashjkasd"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:src="@drawable/conference_room"
                android:layout_weight="1.9"/>

            <TextView
                android:id="@+id/asdlkjl"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Saving Collection"
                android:textColor="#494949"
                android:textSize="13sp"
                android:textStyle="bold"
                android:gravity="center"
                android:layout_weight="0.1"/>
        </LinearLayout>

答案 2 :(得分:1)

如果您想使用Linearlayout符合您的要求,请使用以下代码:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="20dp"
    android:layout_weight="1"
    android:orientation="horizontal"
    android:weightSum="3">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/bottom_border_help"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="10dp"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="15dp"
            android:scaleType="fitCenter"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="HELLO"
            android:textColor="@color/color_black" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/bottom_border_help"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="10dp"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="15dp"
            android:scaleType="fitCenter"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="HELLO"
            android:textColor="@color/color_black" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/bottom_border_help"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="10dp"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="15dp"
            android:scaleType="fitCenter"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="HELLO"
            android:textColor="@color/color_black" />
    </LinearLayout>


</LinearLayout>

<强>输出

enter image description here

答案 3 :(得分:0)

在LinearLayout

中添加此行
android:layout_width="match_parent"
android:gravity="center"

答案 4 :(得分:0)

您可以设置将视图保持居中的线性布局的重力,这样

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:background="#156C7F"
        android:gravity="center"
        android:orientation="horizontal">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="125dp"
            android:text="Branch Office : Dhankuta"
            android:textColor="#FFFFFF"
            android:textSize="10sp" />

        <View
            android:layout_width="1dp"
            android:layout_height="15dp"
            android:layout_gravity="center"
            android:layout_marginLeft="7dp"
            android:background="#FFFFFF" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="7dp"
            android:text="Tuesday - 4 April, 2017"
            android:textColor="#FFFFFF"
            android:textSize="10sp" />


    </LinearLayout> 

这只是意味着linearLayout中的任何子元素都应默认放置在布局的确切中心。当您为linearlayout定义重力时,不需要定义layout_gravity元素,它将自动设置为居中。