【问题标题】:Setting parameter of layout of android programmatically以编程方式设置android布局的参数
【发布时间】:2011-11-20 04:23:50
【问题描述】:

我是安卓新手。我想知道如何从程序中将参数或属性设置为布局 x 和布局 y 宽度和高度,以用于绝对布局等任何布局。

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    如果你像这样为你的布局设置 id

    <LinearLayout android:id="@+id/linear" />
    

    那么你可以像这样在代码中获取布局。

    Linearlayout linear = (LinearLayout)findViewbyId(R.id.linear");
    
    linear.setLayoutParams(new LayoutParams(arg0, arg1));
    

    在 arg0 和 arg1 中你可以传递 int 值,你可以设置下面的值

    LayoutParams.FILL_PARENT
    LayoutParams.WRAP_CONTENT
    

    【讨论】:

    • 谢谢普拉蒂克。我们如何设置按钮的布局。我的意思是说我们在 xml 文件中所做的 x 和 y 坐标。
    • no 不能这样设置按钮,因为没有设置 x 和 y 值的属性。您可以设置边距值以使用 RelativeLayout 将按钮放在特定位置上,也可以通过覆盖 draw() 方法来绘制按钮
    • @Pratik 在这里而不是 fill_parent 我怎样才能在倾角值中设置我的宽度和高度,例如 width= 300.0dip 和 height = 20.05dip?
    【解决方案2】:

    对于按钮,你可以这样尝试:

    RelativeLayout.Layoutparams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
    params.setMargins(5, 5, 5, 5);
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    button.setLayoutParams(params);
    

    你有 setMargins() 分别传递左、上、右和下值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-31
      • 2011-10-11
      • 1970-01-01
      • 2022-10-19
      • 2020-10-30
      相关资源
      最近更新 更多