나는 내 EditText
와 Button
뷰 에이 문제 가 있는데, 텍스트에서 멀리 떨어져있는 좋은 패딩이 있지만 배경을 변경 setBackgroundDrawable
하거나 setBackgroundResource
패딩이 영원히 손실됩니다.
답변
내가 찾은 것은 배경 리소스로 9 패치를 추가하여 패딩을 재설정하는 것입니다. 흥미롭게도 색상 또는 9가 아닌 패치 이미지를 추가하면 그렇지 않았습니다. 해결책은 배경이 추가되기 전에 패딩 값을 저장 한 다음 나중에 다시 설정하는 것입니다.
private EditText value = (EditText) findViewById(R.id.value);
int pL = value.getPaddingLeft();
int pT = value.getPaddingTop();
int pR = value.getPaddingRight();
int pB = value.getPaddingBottom();
value.setBackgroundResource(R.drawable.bkg);
value.setPadding(pL, pT, pR, pB);
답변
요소를 다른 레이아웃 (이 경우 FrameLayout
. FrameLayout
이를 통해 포함 된에있는 패딩을 파괴하지 않고 의 배경을 변경할 수 있었습니다 RelativeLayout
.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/commentCell"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/comment_cell_bg_single" >
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="20dp" >
<ImageView android:id="@+id/sourcePic"
android:layout_height="75dp"
android:layout_width="75dp"
android:padding="5dp"
android:background="@drawable/photoframe"
/>
...
다른 옵션은 Drawable
위에서 제안한대로 배경 을 설정 한 후 프로그래밍 방식으로 설정하는 것 입니다. 픽셀을 계산하여 장치의 해상도를 수정하십시오.
답변
TextView에서이 문제가 발생했기 때문에 TextView를 서브 클래 싱하고 메서드의 Override 메서드를 만들었습니다 TextView.setBackgroundResource(int resid)
. 이렇게 :
@Override
public void setBackgroundResource(int resid) {
int pl = getPaddingLeft();
int pt = getPaddingTop();
int pr = getPaddingRight();
int pb = getPaddingBottom();
super.setBackgroundResource(resid);
this.setPadding(pl, pt, pr, pb);
}
이렇게하면 리소스를 설정하기 전에 항목의 패딩을 가져 오지만 패딩을 유지하는 것 외에는 메서드의 원래 기능을 실제로 엉망으로 만들지 않습니다.
답변
이 슈퍼를 철저히 테스트하지는 않았지만이 방법이 유용 할 수 있습니다.
/**
* Sets the background for a view while preserving its current padding. If the background drawable
* has its own padding, that padding will be added to the current padding.
*
* @param view View to receive the new background.
* @param backgroundDrawable Drawable to set as new background.
*/
public static void setBackgroundAndKeepPadding(View view, Drawable backgroundDrawable) {
Rect drawablePadding = new Rect();
backgroundDrawable.getPadding(drawablePadding);
int top = view.getPaddingTop() + drawablePadding.top;
int left = view.getPaddingLeft() + drawablePadding.left;
int right = view.getPaddingRight() + drawablePadding.right;
int bottom = view.getPaddingBottom() + drawablePadding.bottom;
view.setBackgroundDrawable(backgroundDrawable);
view.setPadding(left, top, right, bottom);
}
view.setBackgroundDrawable (Drawable) 대신 이것을 사용하십시오.
답변
cottonBallPaws의 대답의 역 호환 버전
/**
* Sets the background for a view while preserving its current padding. If the background drawable
* has its own padding, that padding will be added to the current padding.
*
* @param view View to receive the new background.
* @param backgroundDrawable Drawable to set as new background.
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@SuppressWarnings("deprecation")
public static void setBackgroundAndKeepPadding(View view, Drawable backgroundDrawable) {
Rect drawablePadding = new Rect();
backgroundDrawable.getPadding(drawablePadding);
int top = view.getPaddingTop() + drawablePadding.top;
int left = view.getPaddingLeft() + drawablePadding.left;
int right = view.getPaddingRight() + drawablePadding.right;
int bottom = view.getPaddingBottom() + drawablePadding.bottom;
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
view.setBackgroundDrawable(backgroundDrawable);
} else {
view.setBackground(backgroundDrawable);
}
view.setPadding(left, top, right, bottom);
}
답변
9 패치 이미지를 사용하고 드로어 블에서 콘텐츠 영역을 정의하여 약간의 패딩을 제공 할 수 있습니다.
이것을 확인하십시오
xml 또는 프로그래밍 방식으로 레이아웃의 패딩을 설정할 수도 있습니다.
xml 패딩 태그
android:padding
android:paddingLeft
android:paddingRight
android:paddingTop
android:paddingBottom
EditText 또는 Button 뷰에서 setPadding을 호출하여 setBackgroundDrawable을 호출 한 후 코드에서 수동으로 패딩을 설정할 수 있습니다.
답변
일반 검색 자에게는
setBackgroudDrawable 뒤에 setPadding을 추가하기 만하면됩니다. 드로어 블을 변경할 때 setPadding을 다시 호출해야합니다.
처럼:
view.setBackgroundDrawable(backgroundDrawable);
view.setPadding(x, x, x, x);
가장 깨끗한 방법은 drawable-image-file을 가리키는 xml-drawable 내부에 패딩을 정의하는 것입니다.
Greatings