how to set EditText TopMargins in dp programatically?

╄→гoц情女王★ 提交于 2019-12-10 12:51:25

问题


In my android application I want to change the topMargin of an editText.

The thing is that I want to change it "dp" wise and not pixel wise.

I want to change only the topMaring. Leave the other as is. not set them to zeros ?

programmatically i can set margins in int only?


回答1:


set like follw code:

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);

and 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;
}

use follow code for changing just one.

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    params.topMargin = convertPixelsToDp(5,this);
    editText.setLayoutParams(params);



回答2:


LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
editText.setLayoutParams(lp);



回答3:


you can not directly set margins using dp programmatically, because .setMargins method ask pixels not for dp so if you want to give dp's instead of pixels, you should convert dps to pixels. first create LayoutParams instance:

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

then set convert dp to pixels and set margins of the layout like this:

lp.setMargins(left, dpToPx(30), right, bottom);
youredittext.setLayoutParams(lp); 

this is the conversion method from pixels to dp and dp to pixels:

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);
}

cheers,

Hamad




回答4:


this is what I made to show code next to barcode generated by 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());

add this in layout 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>


来源:https://stackoverflow.com/questions/20761611/how-to-set-edittext-topmargins-in-dp-programatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!