【问题标题】:Android RelativeLayout programmatically Set "centerInParent"Android RelativeLayout 以编程方式设置“centerInParent”
【发布时间】:2011-04-28 11:58:42
【问题描述】:

我有一个像这样的 RelativeLayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip">

    <Button
        android:id="@+id/negativeButton"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:textSize="20dip"
        android:textColor="#ffffff"
        android:layout_alignParentLeft="true"
        android:background="@drawable/black_menu_button"
        android:layout_marginLeft="5dip"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"/> 

    <Button
        android:id="@+id/positiveButton"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:textSize="20dip"
        android:textColor="#ffffff"
        android:layout_alignParentRight="true"
        android:background="@drawable/blue_menu_button"
        android:layout_marginRight="5dip"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"/>
</RelativeLayout>

我希望能够以编程方式为positiveButton 设置与以下相同的效果:

android:layout_centerInParent="true"

如何以编程方式进行此操作?

【问题讨论】:

    标签: android android-layout android-relativelayout


    【解决方案1】:

    完全未经测试,但这应该工作:

    View positiveButton = findViewById(R.id.positiveButton);
    RelativeLayout.LayoutParams layoutParams = 
        (RelativeLayout.LayoutParams)positiveButton.getLayoutParams();
    layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
    positiveButton.setLayoutParams(layoutParams);
    

    在清单中的活动中添加android:configChanges="orientation|screenSize"

    【讨论】:

    • 那行得通,但只有在我 indertet layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);在父规则的中心之前。谢谢。
    • 我想补充一点,这也适用于我,但我必须更改 layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, 0);到 layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, -1);在我的情况下。您的应用可能需要“锚”字段中的不同值。
    • 锚字段值可以是除 0 以外的任何值,表示当前为真。 Source 有类似if (rules[CENTER_IN_PARENT] != 0 || rules[CENTER_HORIZONTAL] != 0) { 这样的比较,其中0 有效评估为false
    • 作为后续问题,有谁知道这个答案中的代码是否可以用于动画?例如,将图像从相对左偏移量动画到居中位置等。
    • 你可以简单地使用layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT),第二个参数不需要,你可以在documentation查看
    【解决方案2】:

    只是为了从鲁本响应中添加另一种风格,我这样使用它来根据条件添加或删除此规则:

        RelativeLayout.LayoutParams layoutParams =
                (RelativeLayout.LayoutParams) holder.txtGuestName.getLayoutParams();
    
        if (SOMETHING_THAT_WOULD_LIKE_YOU_TO_CHECK) {
            // if true center text:
            layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
            holder.txtGuestName.setLayoutParams(layoutParams);
        } else {
            // if false remove center:
            layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, 0);
            holder.txtGuestName.setLayoutParams(layoutParams);
        }
    

    【讨论】:

    • 删除规则的最佳方法是:layoutParams.removeRule(RelativeLayout.CENTER_IN_PARENT);
    【解决方案3】:

    我已经完成了

    1。 centerInParent

    2. centerHorizo​​ntal

    3. centerVertical

    truefalse
    private void addOrRemoveProperty(View view, int property, boolean flag){
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
        if(flag){
            layoutParams.addRule(property);
        }else {
            layoutParams.removeRule(property);
        }
        view.setLayoutParams(layoutParams);
    }
    

    如何调用方法:

    centerInParent - 真

    addOrRemoveProperty(mView, RelativeLayout.CENTER_IN_PARENT, true);
    

    centerInParent - 假

    addOrRemoveProperty(mView, RelativeLayout.CENTER_IN_PARENT, false);
    

    centerHorizo​​ntal - true

    addOrRemoveProperty(mView, RelativeLayout.CENTER_HORIZONTAL, true);
    

    centerHorizo​​ntal - false

    addOrRemoveProperty(mView, RelativeLayout.CENTER_HORIZONTAL, false);
    

    centerVertical - 真

    addOrRemoveProperty(mView, RelativeLayout.CENTER_VERTICAL, true);
    

    centerVertical - false

    addOrRemoveProperty(mView, RelativeLayout.CENTER_VERTICAL, false);
    

    希望这会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2014-06-07
      • 1970-01-01
      • 2014-11-21
      • 1970-01-01
      • 1970-01-01
      • 2016-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多