【问题标题】:how to set padding or margin to linear layout?如何将填充或边距设置为线性布局?
【发布时间】:2012-06-02 11:51:34
【问题描述】:

我有一个线性布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_menu"
android:layout_width="fill_parent"
android:layout_height="70px"
android:layout_alignParentBottom="true"
android:background="@drawable/footer_bar"
android:gravity="center" >
</LinearLayout>

当我设置条件时

if (constant.getscreenresolution() >= 800) {
    //i want to make it height * 2
}

那么layout params怎么设置呢?

【问题讨论】:

    标签: android layoutparams


    【解决方案1】:

    填充:

    LinearLayout mLayout = (LinearLayout)findViewById(R.id.layout_menu);
    mLayout.setPadding(left, top, right, bottom);
    

    【讨论】:

      【解决方案2】:
      LinearLayout mLayout = (LinearLayout)v.findViewById(R.id.ll_winfowindo);
      int left= (int) activity.getResources().getDimension(R.dimen._18sdp);
      int top=(int) activity.getResources().getDimension(R.dimen._22sdp);
      int right=(int) activity.getResources().getDimension(R.dimen._18sdp);
      int bottom=(int) activity.getResources().getDimension(R.dimen._34sdp);
      mLayout.setPadding(left, top, right, bottom);
      

      【讨论】:

        【解决方案3】:

        希望对你有帮助

        LinearLayout.LayoutParams layout_param= new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.fill_parent,
                        height * 2);
        mLayout = (LinearLayout) findViewById(R.id.layout_menu);
        mLayout.setLayoutParams(layout_param);
        

        【讨论】:

        • 返回这个异常05-28 13:41:11.125: E/AndroidRuntime(16380): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams 我的布局是线性的,但为什么涉及相对?
        • 艾伦,你应该使用 RelativeLayout 参数而不是 LinearLayout 参数
        【解决方案4】:

        你不应该这样做。首先,您使用像素 (px) 而不是与设备无关的像素 (dp) 这样做会创建仅适用于特定设备屏幕配置的 UI。您需要了解 Android 如何解决屏幕密度的变化,以便您的应用与屏幕密度无关。从这里开始:

        http://developer.android.com/guide/practices/screens_support.html

        【讨论】:

        • 我知道,这就是我创建 2 组布局的原因
        【解决方案5】:

        向线性布局添加边距:-

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
             LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        
        layoutParams.setMargins(30, 20, 30, 0);
        

        【讨论】:

          猜你喜欢
          • 2014-08-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多