【问题标题】:how to set EditText TopMargins in dp programatically?如何以编程方式在 dp 中设置 EditText 上边距?
【发布时间】:2014-01-12 17:43:44
【问题描述】:

在我的 android 应用程序中,我想更改 editText 的 topMargin。

问题是我想改变它“dp”明智而不是像素明智。

我只想更改 topMaring。让另一个保持原样。不将它们设置为零?

以编程方式我只能在 int 中设置边距?

【问题讨论】:

    标签: android android-layout layout margin


    【解决方案1】:

    设置如下代码:

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

    【讨论】:

    【解决方案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】:

    您不能以编程方式直接使用 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);
    }
    

    干杯,

    哈马德

    【讨论】:

      【解决方案4】:

      这是我在 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>
      

      【讨论】:

        猜你喜欢
        • 2012-09-25
        • 1970-01-01
        • 2016-12-23
        • 2011-06-22
        • 2011-01-29
        • 2019-10-19
        • 2023-04-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多