首页 > 解决方案 > 如何在缩放时动态更改 LinearLayout 子级的布局

问题描述

我有 LinearLayout 和 TextView 作为孩子。在 LinearLayout 上的触摸事件中,我将 LinearLayout 的布局从一个值更改为另一个值。但问题是 LinearLayout 放大但孩子保持不变。

Xamarin.Android 项目

私有 LinearLayout 线性;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        SetContentView(Resource.Layout.activity_main);

       linear = (LinearLayout)FindViewById(Resource.Id.parentView);
        linear.Touch += Linear_Touch;
    }

    private void Linear_Touch(object sender, View.TouchEventArgs e)
    {
        {
            linear.Layout(0, 0, (500), (500));
        }
    }


<?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="100dp"
    android:layout_height="100dp"
              android:id="@+id/parentView"
              android:background="@android:color/holo_orange_light">
  <TextView android:layout_width="50dp"
    android:layout_height="50dp"
            android:text="Ashok kumar"
            android:textSize="13dp"
            android:background="@android:color/holo_green_light"/>
</LinearLayout>

预期结果:textView 也随着线性布局的变化而变化

标签: androidxamarinxamarin.android

解决方案


首先,我修改了您有关重置 LinearLayout 布局的代码,然后您可以使用 Layout_weight 来实现您的目标。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"   android:orientation="vertical"   
android:id="@+id/parentlayout"  
android:layout_width="100dp"
android:layout_height="100dp" 
android:background="@android:color/holo_orange_dark">

<TextView 
    android:layout_width="50dp"
    android:layout_height="20dp"
    android:layout_weight="1"
    android:id="@+id/textView1"     
        android:text="Ashok kumar"
        android:textSize="13dp"
        android:background="@android:color/holo_green_light"/>

 </LinearLayout>


 private void Linear_Touch(object sender, Android.Views.View.TouchEventArgs e)
    {

        FrameLayout.LayoutParams _params = new FrameLayout.LayoutParams(500, 500);
        linear.LayoutParameters = _params;
        linear.RequestLayout();

    }

推荐阅读