动态更改relativelayout的布局不会影响relativelayout的子级

时间:2019-02-01 06:22:46

标签: android xamarin xamarin.android

我自定义了viewGroup,其中包含Relativelayout作为直接子级,而Button作为嵌套子级。每当我更改Viewgroup的布局时,我都会使用更改Relativelayout的布局,但这并不影响RelativeLayout的子级。

注意:customViewGroup被添加为mainlayout的子代。

我已经使用按钮在主布局中动态更改布局。

main_activity layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/MainLayout"
    android:background="@android:color/holo_blue_bright" />

CustomViewGroup.java 作为mainlayout

的子级添加
internal class CustomViewGroup : ViewGroup {
    Button m_button;
    private RelativeLayout View;

    internal CustomViewGroup(Context context) : base(context) {
        Initialize(context);
    }

    protected override void OnLayout(bool changed, int l, int t, int r, int b) {
        int count = ChildCount;
        for (int i = 0; i < count; i++) {
            ViewGroup child = GetChildAt(i) as ViewGroup;
            child.Layout(0,0 , (int)(r-l), (int)(b-t));                
        }  
    }

    protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int count = ChildCount;
        for (int i = 0; i < count; i++) {
            View child = GetChildAt(i);
            this.MeasureChildren(widthMeasureSpec, heightMeasureSpec);
        }
        this.SetMeasuredDimension(widthMeasureSpec, heightMeasureSpec);            
    }

    protected override void OnSizeChanged(int w, int h, int oldw, int oldh) {
        base.OnSizeChanged(w, h, oldw, oldh);
    }

    private void Initialize(Context context) {
        this.Layout(100, 100, 500, 500);
        SetBackgroundColor(Color.Gray);
        View = new RelativeLayout(context);
        View.SetBackgroundColor(Color.Green);
        Button button = new Button(context);
        button.SetBackgroundColor(Color.Yellow);
        RelativeLayout.LayoutParams lay = new 
        RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MatchParent, 
        RelativeLayout.LayoutParams.MatchParent);

        button.LayoutParameters = lay;
        button.Text = "Ashok";

        View.AddView(button);
        AddView(View);
    }
}

MainActivity.java

public class MainActivity : AppCompatActivity {
    private CustomViewGroup viewgroup;

    protected override void OnCreate(Bundle savedInstanceState) {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.activity_main);
        LinearLayout parentView = (LinearLayout)FindViewById(Resource.Id.MainLayout);
        parentView.Orientation = Orientation.Vertical;
        Button button = new Button(this);
        button.Click += Button_Click;
        button.Text = "ChangeLayout";             

        viewgroup = new CustomViewGroup(this);
        LinearLayout.LayoutParams lay = new LinearLayout.LayoutParams(300,300);
        viewgroup.LayoutParameters = lay;

        parentView.AddView(viewgroup);
        parentView.AddView(button);             
    }

    private void Button_Click(object sender, EventArgs e) {
        viewgroup.Layout(0, 0, 500, 500);
    }
}

1 个答案:

答案 0 :(得分:0)

更改此

viewgroup.Layout(0, 0, 500, 500);

进入此

LinearLayout.LayoutParams lay = new LinearLayout.LayoutParams(500,500); 
viewgroup.LayoutParameters = lay;