簡體   English   中英

ImageView 留下不需要的頂部/底部邊距/填充

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

我正在設計一個 Android XML 布局,其中包含一些代表復選標記和紅色 X 圖像的 ImageViews。 當這些圖像顯示在屏幕上時,我遇到了一些包含額外邊距/填充的問題。

填充問題的屏幕截圖

請注意,似乎沒有右側或左側邊距/填充,但有大量的頂部/底部填充。

圖像是 PNG 格式,遵循http://developer.android.com/design/style/iconography.html 中描述的創建小/上下文圖標的指南

我想通過讓邊距/填充與其水平邊距/填充的效果相匹配來糾正這個問題。

這是布局的 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>

這是填充 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());
}

剛剛通過另一個問題找到了解決方案: Unwanted padding around an ImageView

ImageView XML 對象上的android:adjustViewBounds="true"解決了問題。

好吧,您也可以執行以下操作

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

使用 RelativeLayout 而不是 LinearLayout。 此外,除非必要,否則盡量避免使用重量等屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM