如何减轻体重?

时间:2018-10-28 15:59:57

标签: android xml android-layout-weight

我使用layout_weight来设置布局。添加照片按钮将允许用户选择图像。选择后,它将显示以下图像:

Screenshot of "My Application" with enlarged "like" button

没有图像,ImageView的可见性将设置为View.GONE,我希望它以这种方式显示(只需删除ImageView并向上移动`EditText)

Screenshot of "My Application" without "like" button (expected result)

相反,它显示如下:

Screenshot of "My Application" with bad layout (actual result)

所以我的问题是:实现这一目标的更好方法是什么?

这是布局XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_marginLeft="10dp"
              android:layout_marginRight="10dp"
              android:layout_width="match_parent" android:layout_height="match_parent"
              android:orientation="vertical">

    <LinearLayout
            android:layout_marginTop="5dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="bottom"
            android:orientation="vertical">

    <ImageView android:layout_width="match_parent" android:layout_height="0dip"
               android:layout_centerHorizontal="true"
               android:src="@drawable/like"
               android:tint="@color/darkGreen"
               android:layout_weight="2.5"
               android:scaleType="centerCrop"
               android:adjustViewBounds="true"
               android:layout_marginRight="10dp"
               android:id="@+id/imageView"
               android:layout_marginTop="10dp"/>


    <EditText android:layout_width="match_parent" android:layout_height="0dip"
              android:layout_weight="2.5"
              android:paddingTop="10dp"
              android:paddingLeft="10dp"
              android:id="@+id/commentEditTxt"
              android:gravity="top"
              android:layout_marginTop="10dp"
              android:hint="Write something here ..."
              android:background="@drawable/round_button"/>

        <Button
                android:layout_weight="0.8"
                android:drawableTint="@color/darkgrey"
                android:paddingRight="20dp"
                android:textAllCaps="false"
                android:drawableRight="@drawable/add_photo"
                android:textColor="@color/darkgrey"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="10dp"
                android:id="@+id/addPhotoBtn"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="match_parent"
                android:layout_height="0dip"
                android:background="@drawable/round_button"
                android:layout_gravity="center_horizontal|center"
                android:text="add a photo"/>

        <Button
                android:layout_weight="0.8"
                android:backgroundTint="@color/lightseagreen"
                android:textColor="@color/white"
                android:textAllCaps="false"
                android:layout_marginBottom="10dp"
                android:background="@drawable/round_button"
                android:id="@+id/doneBtn"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="match_parent"
                android:layout_height="0dip"
                android:layout_gravity="center_horizontal|center"
                android:text="Done"/>
    </LinearLayout>
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

很抱歉更改代码,但是现在可以根据需要运行了。您的imageView现在不可见。当用户点击并选择图片时,您应该使用此图片:

// imageView.setVisibility(View.VISIBLE);

<?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:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:gravity="bottom"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_centerHorizontal="true"
            android:src="@drawable/like"
            android:tint="@color/darkGreen"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"
            android:visibility="gone" />


        <EditText
            android:id="@+id/commentEditTxt"
            android:layout_width="match_parent"

            android:layout_height="200dp"
            android:layout_marginTop="10dp"
            android:focusedByDefault="false"
            android:gravity="top"
            android:hint="Write something here ..."
            android:paddingLeft="10dp"
            android:paddingTop="10dp"
            android:background="@drawable/round_button"/>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <Button


                android:id="@+id/addPhotoBtn"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:drawableTint="@color/darkgrey"
                android:drawableRight="@drawable/add_photo"
                android:textColor="@color/darkgrey"
                android:background="@drawable/round_button"
                android:layout_above="@id/doneBtn"
                android:layout_marginTop="10dp"
                android:paddingRight="20dp"
                android:text="add a photo"
                android:textAllCaps="false" />

            <Button

                android:id="@+id/doneBtn"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="10dp"
                android:text="Done"
                android:background="@drawable/round_button"
                android:backgroundTint="@color/lightseagreen"
                android:textColor="@color/white"
                android:textAllCaps="false" />
        </RelativeLayout>
    </LinearLayout>
</LinearLayout>

答案 1 :(得分:0)

我将EditText和ImageView放在LinearLayout中,尝试一下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="bottom"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="5">

            <ImageView android:layout_width="match_parent" android:layout_height="0dip"
                android:layout_centerHorizontal="true"
                android:src="@drawable/like"
                android:tint="@color/darkGreen"
                android:layout_weight="1"
                android:scaleType="centerCrop"
                android:adjustViewBounds="true"
                android:layout_marginRight="10dp"
                android:id="@+id/imageView"
                android:layout_marginTop="10dp" />


            <EditText android:layout_width="match_parent" android:layout_height="0dip"
                android:layout_weight="1"
                android:paddingTop="10dp"
                android:paddingLeft="10dp"
                android:id="@+id/commentEditTxt"
                android:gravity="top"
                android:layout_marginTop="10dp"
                android:hint="Write something here ..."
                android:background="@drawable/round_button"/>
        </LinearLayout>

        <Button
            android:layout_weight="0.8"
            android:drawableTint="@color/darkgrey"
            android:paddingRight="20dp"
            android:textAllCaps="false"
            android:drawableRight="@drawable/add_photo"
            android:textColor="@color/darkgrey"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp"
            android:id="@+id/addPhotoBtn"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:background="@drawable/round_button"
            android:layout_gravity="center_horizontal|center"
            android:text="add a photo"/>

        <Button
            android:layout_weight="0.8"
            android:backgroundTint="@color/lightseagreen"
            android:textColor="@color/white"
            android:textAllCaps="false"
            android:layout_marginBottom="10dp"
            android:background="@drawable/round_button"
            android:id="@+id/doneBtn"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_gravity="center_horizontal|center"
            android:text="Done"/>
    </LinearLayout>
</LinearLayout>