設定方法は証拠金のImageViewコードはxml
質問
付け加えておきたい未知数 ImageView
眺望自分のレイアウト。XMLで使用ができます layout_margin
このように:
<ImageView android:layout_margin="5dip" android:src="@drawable/image" />
はありま ImageView.setPadding()
, がない ImageView.setMargin()
.と思っている ImageView.setLayoutParams(LayoutParams)
, などのように飼料を入れる。
なんだろうけど、日本人?
解決
android.view.ViewGroup.MarginLayoutParams
は、メソッドsetMargins(left, top, right, bottom)
を持っています。直接のサブクラスは以下のとおりです。FrameLayout.LayoutParams
、LinearLayout.LayoutParams
とRelativeLayout.LayoutParams
の使用例LinearLayout
ます:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
imageView.setLayoutParams(lp);
これはピクセル単位でマージンを設定します。スケールには、
を使用しますcontext.getResources().getDisplayMetrics().density
他のヒント
image = (ImageView) findViewById(R.id.imageID);
MarginLayoutParams marginParams = new MarginLayoutParams(image.getLayoutParams());
marginParams.setMargins(left_margin, top_margin, right_margin, bottom_margin);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
image.setLayoutParams(layoutParams);
上記のすべての実施例は、実際の の所望されないかもしれないビューのための本既に任意のparamsに置き換えられます。以下のコードは、ちょうどそれらを置き換えることなく、既存のparamsを拡張します。
ImageView myImage = (ImageView) findViewById(R.id.image_view);
MarginLayoutParams marginParams = (MarginLayoutParams) image.getLayoutParams();
marginParams.setMargins(left, top, right, bottom);
ケビンのコードは、冗長MarginLayoutParams
オブジェクトを作成します。簡単なバージョン:
ImageView image = (ImageView) findViewById(R.id.main_image);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(image.getLayoutParams());
lp.setMargins(50, 100, 0, 0);
image.setLayoutParams(lp);
変更したい場合はimageview率が他のすべてのマージンはそのままにします。
車MarginLayoutParametersのイメージビューこの場合:
myImageView
MarginLayoutParams marginParams = (MarginLayoutParams) myImageView.getLayoutParams();
今だけの変更は、証拠金を変更したいものとしてい
marginParams.setMargins(marginParams.leftMargin, marginParams.topMargin, 150, //notice only changing right margin marginParams.bottomMargin);
あなたがこの方法を使用することができ、場合にあなたがDPで余白を指定します。
private void addMarginsInDp(View view, int leftInDp, int topInDp, int rightInDp, int bottomInDp) {
DisplayMetrics dm = view.getResources().getDisplayMetrics();
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.setMargins(convertDpToPx(leftInDp, dm), convertDpToPx(topInDp, dm), convertDpToPx(rightInDp, dm), convertDpToPx(bottomInDp, dm));
view.setLayoutParams(lp);
}
private int convertDpToPx(int dp, DisplayMetrics displayMetrics) {
float pixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, displayMetrics);
return Math.round(pixels);
}
私は単にこれを使用して素晴らしい作品ます:
ImageView imageView = (ImageView) findViewById(R.id.image_id);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
layoutParams.setMargins(left, top, right, bottom);
imageView.setLayoutParams(layoutParams);
setMargins()の単位はDPない画素です。
:あなたはDPにセットマージンにしたい場合は、ちょうどあなたのの値/ dimens.xml のファイルの中のようなあなたのディメンションを作成<resources>
<dimen name="right">16dp</dimen>
<dimen name="left">16dp</dimen>
</resources>
などアクセス:
getResources().getDimension(R.dimen.right);
動的レイアウト作成とImageViewの上で直接動作しません)(setmarginとしてのパラメータを設定する
ImageView im;
im = (ImageView) findViewById(R.id.your_image_in_XML_by_id);
RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(im.getLayoutParams());
layout.setMargins(counter*27, 0, 0, 0);//left,right,top,bottom
im.setLayoutParams(layout);
im.setImageResource(R.drawable.yourimage)
これは働いていた私にとってます:
int imgCarMarginRightPx = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, definedValueInDp, res.getDisplayMetrics());
MarginLayoutParams lp = (MarginLayoutParams) imgCar.getLayoutParams();
lp.setMargins(0,0,imgCarMarginRightPx,0);
imgCar.setLayoutParams(lp);
あなたはkotlin使用している場合、これは拡張機能を作成することによって単純化することができる。
fun View.setMarginExtensionFunction(left: Int, top: Int, right: Int, bottom: Int) {
val params = layoutParams as ViewGroup.MarginLayoutParams
params.setMargins(left, top, right, bottom)
layoutParams = params
}
今、すべてのあなたが必要とするが、図であり、この拡張機能はどこでも使用することができます。
val imageView = findViewById(R.id.imageView)
imageView.setMarginExtensionFunction(0, 0, 0, 0)
サンプルコードは、ここにあるのは非常に簡単。
LayoutParams params1 = (LayoutParams)twoLetter.getLayoutParams();//twoletter-imageview
params1.height = 70;
params1.setMargins(0, 210, 0, 0);//top margin -210 here
twoLetter.setLayoutParams(params1);//setting layout params
twoLetter.setImageResource(R.drawable.oo);
これと同様の方法を使用するには、あなたにいくつかの状況では、いくつかの頭痛の種を保存することがあります。 あなたは余白とprogrammatical工夫の二つのパスを持っている場合には、いくつかのlayoutParamsセットがすでに存在しているかどうかを確認する方が安全です。いくつかのマージンがすでに存在する場合は1がそれらを増加し、それらを置き換えるべきではありません。
public void addMargins(View v, int left, int top, int right, int bottom) {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) v.getLayoutParams();
if (params == null)
params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
int oldLeft = params.leftMargin;
int oldTop = params.topMargin;
int oldRight = params.rightMargin;
int oldBottom = params.bottomMargin;
params.setMargins(oldLeft + left, oldTop + top, oldRight + right, oldBottom + bottom);
v.setLayoutParams(params);
}
ここで左、上、右、下に8pxマージンを追加する例です。
ImageView imageView = new ImageView(getApplicationContext());
ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(
ViewGroup.MarginLayoutParams.MATCH_PARENT,
ViewGroup.MarginLayoutParams.WRAP_CONTENT
);
marginLayoutParams.setMargins(8, 8, 8, 8);
imageView.setLayoutParams(marginLayoutParams);