简体   繁体   English

ImageView 留下不需要的顶部/底部边距/填充

[英]ImageView leaves unwanted top/bottom margin/padding

I am designing an Android XML layout that contains a few ImageViews representing images of checkmarks and red Xs.我正在设计一个 Android XML 布局,其中包含一些代表复选标记和红色 X 图像的 ImageViews。 I have run into a few issues with these images containing extra margin/padding when presented to the screen.当这些图像显示在屏幕上时,我遇到了一些包含额外边距/填充的问题。

填充问题的屏幕截图

Notice that there appears to be no Right or Left margin/padding but there is plenty of top/bottom padding.请注意,似乎没有右侧或左侧边距/填充,但有大量的顶部/底部填充。

The images are PNG formats following the guidelines for creating Small/Context Icons described at http://developer.android.com/design/style/iconography.html图像是 PNG 格式,遵循http://developer.android.com/design/style/iconography.html 中描述的创建小/上下文图标的指南

I would like to correct the issue by having the margin/padding match the effect of its horizontal margin/padding.我想通过让边距/填充与其水平边距/填充的效果相匹配来纠正这个问题。

Here is the XML of the layout:这是布局的 XML:

<!-- Event Reoccuring -->
            <LinearLayout
                android:id="@+id/event_reoccur_row"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:paddingTop="5dp"
                android:paddingLeft="16dp"
                android:paddingRight="16dp"
                >

                <TextView 
                    android:id="@+id/kid_event_reoccur_label"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.5"
                    android:layout_gravity="center_vertical"
                    android:text="@string/event_reoccur"
                />

                <ImageView 
                    android:id="@+id/kid_event_reoccur_indicator"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.1"
                    android:layout_gravity="center_vertical"
                    android:contentDescription="@string/indicator"
                    />

                <TextView 
                    android:id="@+id/kid_event_reoccur"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.4"
                    android:layout_gravity="center_vertical"
                    android:paddingLeft="4dp"
                    android:text="@string/not_applicable"
                />
            </LinearLayout>

<!-- Age Required -->
            <LinearLayout
                android:id="@+id/event_age_req_row"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:paddingLeft="16dp"
                android:paddingRight="16dp"
                >

                <TextView 
                    android:id="@+id/kid_event_age_req_label"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.5"
                    android:layout_gravity="center_vertical"
                    android:text="@string/event_age_req"
                />
                <ImageView 
                    android:id="@+id/kid_event_age_req_indicator"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.1"
                    android:gravity="center_vertical"
                    android:contentDescription="@string/indicator"
                    />
                <TextView 
                    android:id="@+id/kid_event_age_req"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.4"
                    android:layout_gravity="center_vertical"
                    android:paddingLeft="4dp"
                    android:text="@string/not_applicable"
                />
            </LinearLayout>

Here is the code that populates the ImageViews (Note - mReoccurIndcator & mAgeReqIndicator are ImageViews while mReoccurence & mAgeReq are TextViews):这是填充 ImageViews 的代码(注意 - mReoccurIndcator & mageReqIndicator 是 ImageViews 而 mReoccurence & mAgeReq 是 TextViews):

@TargetApi(16)
private void setupEventReoccurence(View v) {
    // Get Reference
    mReoccurence = (TextView) v.findViewById(R.id.kid_event_reoccur);
    mReoccurence.setText(boolToString(mEvent.isReoccuring()));

    // Checkmark/X indicator
    mReoccurIndicator = (ImageView) v.findViewById(R.id.kid_event_reoccur_indicator);
    mReoccurIndicator.setImageResource(R.drawable.greencheck_small_context_icon);
    mReoccurIndicator.setScaleType(ScaleType.CENTER_INSIDE);
    mReoccurIndicator.setCropToPadding(true);
    //mReoccurIndicator.setMaxHeight((int) mReoccurence.getTextSize());
    //mReoccurIndicator.setMinimumHeight((int) mReoccurence.getTextSize());

    Log.d(TAG, "getTextSize() as float: " + mReoccurence.getTextSize());
    Log.d(TAG, "getTextSize() as int: " + (int) mReoccurence.getTextSize());



}
    @TargetApi(16)
private void setupEventAgeReq(View v) {
    // Get Reference
    mAgeReq = (TextView) v.findViewById(R.id.kid_event_age_req);
    mAgeReq.setText(boolToString(mEvent.isAgeReq()));

    // Checkmark/X indicator
    mAgeReqIndicator = (ImageView) v.findViewById(R.id.kid_event_age_req_indicator);
    mAgeReqIndicator.setImageResource(R.drawable.greencheck_small_context_icon);
    mAgeReqIndicator.setScaleType(ScaleType.CENTER_INSIDE);
    mAgeReqIndicator.setCropToPadding(true);
    //mAgeReqIndicator.setMaxHeight((int) mReoccurence.getTextSize());
    //mAgeReqIndicator.setMinimumHeight((int) mReoccurence.getTextSize());

            Log.d(TAG, "getTextSize() as float: " + mReoccurence.getTextSize());
            Log.d(TAG, "getTextSize() as int: " + (int) mReoccurence.getTextSize());
}

Just found a solution through another question: Unwanted padding around an ImageView刚刚通过另一个问题找到了解决方案: Unwanted padding around an ImageView

android:adjustViewBounds="true" on the ImageView XML object solves issue. ImageView XML 对象上的android:adjustViewBounds="true"解决了问题。

Well you can also do something like below好吧,您也可以执行以下操作

ImageView image = new ImageView(this);
image.setAdjustViewBounds(true);
image.setImageResource(R.drawable.image_file);

Use RelativeLayout instead of LinearLayout.使用 RelativeLayout 而不是 LinearLayout。 Also, try to avoid using attributes like weight unless it is necessary.此外,除非必要,否则尽量避免使用重量等属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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