【发布时间】:2014-01-12 17:43:44
【问题描述】:
在我的 android 应用程序中,我想更改 editText 的 topMargin。
问题是我想改变它“dp”明智而不是像素明智。
我只想更改 topMaring。让另一个保持原样。不将它们设置为零?
以编程方式我只能在 int 中设置边距?
【问题讨论】:
标签: android android-layout layout margin
在我的 android 应用程序中,我想更改 editText 的 topMargin。
问题是我想改变它“dp”明智而不是像素明智。
我只想更改 topMaring。让另一个保持原样。不将它们设置为零?
以编程方式我只能在 int 中设置边距?
【问题讨论】:
标签: android android-layout layout margin
设置如下代码:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, convertPixelsToDp(5,this), 0, 0);
editText.setLayoutParams(lp);
和convert Pixels To Dp
public static int convertPixelsToDp(float px, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
int dp = px / (metrics.densityDpi / 160f);
return dp;
}
使用跟随代码只更改一个。
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.topMargin = convertPixelsToDp(5,this);
editText.setLayoutParams(params);
【讨论】:
Layout Params 并设置为您的EditText。
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
editText.setLayoutParams(lp);
【讨论】:
您不能以编程方式直接使用 dp 设置边距,因为.setMargins 方法要求像素不是 dp,所以如果您想给出 dp 而不是像素,您应该将 dps 转换为像素。
首先创建 LayoutParams 实例:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
然后将convert dp设置为像素并设置布局的边距,如下所示:
lp.setMargins(left, dpToPx(30), right, bottom);
youredittext.setLayoutParams(lp);
这是像素到dp和dp到像素的转换方法:
public static int dpToPx(int dp)
{
return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}
public static int pxToDp(int px)
{
return (int) (px / Resources.getSystem().getDisplayMetrics().density);
}
干杯,
哈马德
【讨论】:
这是我在 zxing 生成的条形码旁边显示的代码:
final TextView ticketCodeTV = (TextView) dialogView.findViewById(R.id.ticketCodeTV);
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.rightMargin = -(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 135, getResources().getDisplayMetrics());
lp.topMargin = Math.round((size.y / 2) - (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 90, getResources().getDisplayMetrics()));
Log.d("rightMargin", Integer.toString(lp.rightMargin));
ticketCodeTV.setLayoutParams(lp);
ticketCodeTV.setText(promo.getTicketCode());
在布局xml中添加:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=""
android:background="#ffff"
android:textColor="#000000"
android:layout_marginBottom="20dp"
android:rotation="-90"
android:textSize="25sp"
android:textStyle="normal"
android:paddingLeft="70dp"
android:paddingRight="70dp"
android:id="@+id/ticketCodeTV"
/>
</RelativeLayout>
【讨论】: