【问题标题】:Android View same Width and HeightAndroid查看相同的宽度和高度
【发布时间】:2016-06-28 06:37:28
【问题描述】:

以下是我的 xml 代码。在我的LinearLayout。我有两个子视图,它们使用weightSum 对齐。我有一个ImageView,由于weightSum,它的宽度正确对齐,但问题在于它的高度。我希望它的高度与其宽度相同。 有没有不使用静态dp 值的解决方案。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp">

    <LinearLayout
        android:background="@drawable/background_gradien_yellow"
        android:weightSum="10"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

            <ImageView
                android:layout_weight="6"
                android:id="@+id/startbtn"
                android:textStyle="bold"
                android:alpha="0.8"
                android:textColor="#fff"
                android:text="@string/start"
                android:layout_centerInParent="true"
               android:background="@drawable/background_gradient_circular"
                android:layout_width="match_parent"
                android:layout_height="150dp"/>


        <LinearLayout
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp"
            android:orientation="vertical"
            android:layout_weight="3"
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <TextView
                android:textColor="@color/White"
                android:id="@+id/tv_challange_title"
                android:textSize="20sp"
                android:text="Early-Bird Challange"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <TextView
                android:id="@+id/tv_challange_desc"
                android:alpha="0.5"
                android:textColor="@color/White"
                android:textSize="15sp"
                android:layout_marginTop="5dp"
                android:text="Take 100 steps five times within.. "
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>


        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

【问题讨论】:

    标签: android xml android-layout


    【解决方案1】:

    使用自定义 ImageView 找到解决方案。 它是最好的解决方案。在自定义 ImageView 类中设置高度与宽度相同。

    Java 代码:

    public class SquareImageView extends ImageView {
    
        public SquareImageView(Context context) {
            super(context);
        }
    
        public SquareImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
            int width = getMeasuredWidth();
            setMeasuredDimension(width, width);
        }
    
    }
    

    XML 代码

             <com.mypakage.utils.SquareImageView
                android:background="@drawable/fitbit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    

    【讨论】:

    • 如果是带孩子的布局怎么办?此代码将从父布局中删除它们
    【解决方案2】:

    我认为您可以使用 Activity 的 Java 代码来实现。

    类似这样的:

    android.view.ViewGroup.LayoutParams mParams = imageView.getLayoutParams();
    mParams.height = imageView.getWidth();
    imageView.setLayoutParams(mParams);
    

    【讨论】:

    • 这也是我想到的,我认为在 xml 代码中没有办法做到这一点,因为权重只能包括单一方向。如果您正确执行此代码,它将起作用。如果你得到一个零宽度,那么只需在 post runnable 方法或 getViewTreeObserver 中添加它
    【解决方案3】:

    自定义ImageView,然后将其height 设置为与width 相同。 check this answer

    【讨论】:

    • 它扩展了我的父视图。使用自定义 ImageView
    • 将 imageView scaleType 设置为“center”
    【解决方案4】:

    您将 ImageView 的 scaleType 设置为 centerInCrop

    android:scaleType="centerInCrop"

    【讨论】:

    • 这与问题有什么关系?
    • 他只是想设置ImageView的Height和Width
    • 但这个答案与调整 imageview 的高度和宽度无关
    • @SecretCoder 更多评论去聊天,更多评论之类的可以去聊天室,所以不适合聊天
    • @Smit.Satodia 我们不是在聊天,而是在讨论。如需更多关注,请寻求帮助。
    【解决方案5】:

    你也可以试试这个,只是给内部线性布局赋予权重

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingBottom="5dp"`enter code here`
        android:paddingLeft="5dp"
        android:paddingRight="5dp">
    
        <LinearLayout
            android:background="@color/colorPrimary"
            android:weightSum="10"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <ImageView
                android:layout_weight="6"
                android:id="@+id/startbtn"
                android:textStyle="bold"
                android:alpha="0.8"
                android:textColor="#fff"
                android:text="start"
                android:layout_centerInParent="true"
                android:background="@drawable/common_ic_googleplayservices"
                android:layout_width="match_parent"
                android:layout_height="150dp"/>
    
    
            <LinearLayout
                android:layout_marginTop="20dp"
                android:layout_marginBottom="20dp"
                android:orientation="vertical"
                android:layout_weight="3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:weightSum="2">
    
    
                <TextView
                    android:textColor="#fff"
                    android:id="@+id/tv_challange_title"
                    android:textSize="20sp"
                    android:text="Early-Bird Challange"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"/>
    
                <TextView
                    android:id="@+id/tv_challange_desc"
                    android:alpha="0.5"
                    android:textColor="#000"
                    android:textSize="15sp"
                    android:layout_marginTop="5dp"
                    android:text="Take 100 steps five times within.. "
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"/>
    
    
            </LinearLayout>
    
        </LinearLayout>
    
    </RelativeLayout>
    

    【讨论】:

      【解决方案6】:

      在宽度和高度上使用相同大小的图像(更好的 .9patch)

            <ImageView
              android:layout_weight="6"
              android:id="@+id/startbtn"
              android:textStyle="bold"
              android:alpha="0.8"
              android:textColor="#fff"
              android:text="start"
              android:layout_centerInParent="true"
              android:layout_width="match_parent"
      
              android:src="@drawable/background_gradient_circular"
              android:scaleType="fitCenter"
              android:layout_height="match_parent" />
      

      【讨论】:

        猜你喜欢
        • 2017-03-31
        • 2015-02-17
        • 1970-01-01
        • 1970-01-01
        • 2017-03-14
        • 1970-01-01
        • 1970-01-01
        • 2014-03-15
        • 1970-01-01
        相关资源
        最近更新 更多