如何将重力应用于“线性布局”中的项目,其方向为“水平”?

时间:2016-07-28 11:28:27

标签: android android-layout android-imageview android-image

我有一个父线性布局,其方向为vertical,在此我有另一个方向为vertical的布局,并且它有一个方向为Horizontal的子布局。

此布局有2个Image Views。 ,我想将重力应用于这些图像views。1使用left-gravity,1使用right-gravity

当我将重力应用于此布局时,它不会产生任何效果,但如果我删除最后horizontal布局,则它完全有效,但会显示1张图像和1张图像。

这是我的代码,请指导我

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#b9000000"
    android:id="@+id/ll_black_overlay"
    >

   //There are 2 More layouts, whith weights 20 and 50

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="30"
        android:orientation="vertical"
        android:paddingTop="20dp"
        android:paddingLeft="70dp"
        android:paddingRight="70dp"
        >

        // Layout for displaying text messages Vertically

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="center"

            >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Text View 1 , "
                android:textColor="@color/white"
                android:layout_gravity="center"
                android:gravity="center"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Text View 002 , "
                android:textColor="@color/white"
                android:layout_gravity="center"
                android:gravity="center"
                />


 </LinearLayout>

  // --Problem area Layout for displaying Images side by side,
      1 image at left corner, 1 at right corner, if i remove Linear layout,
      gravity works, but id is not working with Linear Layout 

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"

            >

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/icn_tour_arrow_left"
                android:layout_gravity="left"
                />


            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/icn_tour_arrow_right"
                android:layout_gravity="right"
                />

        </LinearLayout>

    </LinearLayout>




</LinearLayout>

4 个答案:

答案 0 :(得分:2)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_black_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#b9000000"
android:orientation="vertical">

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Text View 1 , "
        android:textColor="@color/white" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Text View 002 , "
        android:textColor="@color/white" />

</LinearLayout>

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/linearLayout"
    android:src="@drawable/icn_tour_arrow_right" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/linearLayout"
    android:layout_gravity="right"
    android:layout_toLeftOf="@+id/imageView"
    android:layout_toStartOf="@+id/imageView"
    android:src="@drawable/icn_tour_arrow_left"/>

答案 1 :(得分:1)

使用以下布局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_black_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#b9000000"
android:orientation="vertical">

//There are 2 More layouts, whith weights 20 and 50

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="30"
    android:orientation="vertical"
    android:paddingLeft="70dp"
    android:paddingRight="70dp"
    android:paddingTop="20dp">

    // Layout for displaying text messages Vertically

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="vertical"

        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="Text View 1 , "
            android:textColor="@color/white" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="Text View 002 , "
            android:textColor="@color/white" />


    </LinearLayout>

    // --Problem area Layout for displaying Images side by side,
    1 image at left corner, 1 at right corner, if i remove Linear layout,
    gravity works, but id is not working with Linear Layout

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="left">

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@mipmap/ic_launcher" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right">

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@mipmap/ic_launcher" />
        </LinearLayout>

    </LinearLayout>

</LinearLayout>

答案 2 :(得分:1)

  

只需在ImageViews之间添加权重为1的LinearLayout。

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

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/icn_tour_arrow_left"
            android:layout_gravity="left"/>
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"/>
        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/icn_tour_arrow_right"
            android:layout_gravity="right"/>

    </LinearLayout>
</LinearLayout>

答案 3 :(得分:0)

 const Fs = require('fs');

 const readFile = (filename, encoding) => {

     try {
         return Fs.readFileSync(filename).toString(encoding);
     }
     catch (e) {
         return null;
     }
 };

 const contents = readFile(./somefile', 'utf8');
 // contents will be null if it doesn't exist or a string of utf8 if it does 

它将显示两个与屏幕相同的图像 如果你想在角落处的两个图像在layout_weight

的两个图像之间添加另一个文本视图