使子视图与父滚动视图的宽度匹配 [英] Make a child view match the width of a parent scrollview

查看:66
本文介绍了使子视图与父滚动视图的宽度匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有许多EditText子级的水平滚动视图.我希望这些孩子中的每一个都具有与父滚动视图可见区域相同的宽度.在XML中这可能吗?

I have a horizontal scrollview with many EditText children. I want each of these children to be the same width of the visible area of the parent scrollview. Is this possible in XML?

推荐答案

您可以编写一个小的帮助程序类:

You can do it writing a small helper class:

我们正在创建一个很小的类,它扩展了 EditText ,称为 FullWidthEditText ,如下所示:

We are creating a very small class that extends EditText called FullWidthEditText like this:

package com.YOURPACKAGENAME;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.HorizontalScrollView;

public class FullWidthEditText extends EditText {
    public FullWidthEditText(Context context) { super(context);}    
    public FullWidthEditText(Context context, AttributeSet attrs) { super(context, attrs); }
    public FullWidthEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        View parentScrollView=((View)(getParent().getParent()));

        if (parentScrollView!=null) {
            // check the container of the container is an HorizontallScrollView
            if (parentScrollView instanceof HorizontalScrollView) {
                // Yes it is, so change width to HSV's width
                widthMeasureSpec=parentScrollView.getMeasuredWidth();
            }
        }
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
    }
}

现在您已经创建了此类,您可以像常规EditText一样在XML中使用它:

Now you have this class created, you can use it in your XML just like a normal EditText:

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/horizontalScrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffff0000" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_marginRight="5dp"
        android:orientation="horizontal" >

        <com.YOURPACKAGENAME.FullWidthEditText
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/yourEditTextId">
        </com.YOURPACKAGENAME.FullWidthEditText>

        <com.YOURPACKAGENAME.FullWidthEditText
            android:layout_width="wrap_content"
            android:layout_height="match_parent" >
        </com.YOURPACKAGENAME.FullWidthEditText>
        .
        .
        .
    </LinearLayout>
</HorizontalScrollView>

您还可以以编程方式创建它,添加处理程序、侦听器、更改文本、findViewById ……等等……就像普通的 EditText 一样.

You can also create it programmatically, add handlers, listeners, change text, findViewById ... etc ... just like a normal EditText.

 EditText editText=new EditText(context);
 FullWidthEditText fullWidthEditText=new FullWidthEditText(context);

  • 请注意,此解决方案也适用于任何其他Widget.如果您需要ie.一个全角按钮,只需将 extends EditText 更改为 extends Button 即可.

    您可以轻松自定义大小,关键行是: widthMeasureSpec = parentScrollView.getMeasuredWidth(); 这样就可以了.使全宽减去10或20px,使半宽等.

    You can easily customize the size, the key line is: widthMeasureSpec=parentScrollView.getMeasuredWidth(); so you can ie. make full width minus 10 or 20px, make half width, etc.

    希望有帮助!

    这篇关于使子视图与父滚动视图的宽度匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆