android - 如何在缩放时动态更改 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 也随着线性布局的变化而变化
解决方案
首先,我修改了您有关重置 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();
}
推荐阅读
- javascript - 从 html 中删除 jquery 中的元素的功能无法正常工作
- java - org.xml.sax.SAXException:更新“CVE-2018-17481”时出错
- node.js - 使用gridfs上传数组时如何设置自定义字段
- c# - 从继承具体类对抽象类进行类型推断
- javascript - 在 Laravel 5.7 事件监听器中运行 Javascript
- python - 如何使用具有以下限制的 ipython 获得二阶连接?
- azure-cli - 使用 jmespath 过滤嵌套数组(使用 az cli)
- node.js - 在另一个模块的对象内模拟函数(节点+笑话)
- python - Scrapy 在抓取一长串 url 时卡住了
- amazon-web-services - 在 AWS Cloudfront 中如何添加缓存控制?