我有一个包含许多EditText子项的水平滚动视图。我希望这些子节点中的每一个都与父卷轴视图的可见区域的宽度相同。这可能是XML吗?
答案 0 :(得分:8)
你可以写一个小帮手类:
我们正在创建一个非常小的类,扩展名为EditText
的{{1}},如下所示:
FullWidthEditText
现在您已经创建了这个类,您可以像在普通的EditText中一样在XML中使用它:
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);
}
}
你也可以通过编程方式创建它,添加处理程序,监听器,更改文本,findViewById ......等......就像普通的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>
请注意,此解决方案也适用于任何其他Widget。如果你需要ie。一个全角按钮,只需更改 EditText editText=new EditText(context);
FullWidthEditText fullWidthEditText=new FullWidthEditText(context);
的{{1}}就可以了。
您可以轻松自定义尺寸,关键线是:extends EditText
所以你可以。使全宽减去10或20px,制作半宽等等。
希望它有所帮助!
答案 1 :(得分:1)
我认为ViewPager
应该更合适,但也许您可以尝试此视图层次结构:
HorizontalScrollView
(width = MATCH_PARENT
, weightSum = 1
)
LinearLayout
(orientation = HORIZONTAL
, weight = 3
, weightSum = 3
)
LinearLayout
(第1页,orientation = VERTICAL
, weight = 1
)
EditText
(width = MATCH_PARENT
)LinearLayout
(第2页,orientation = VERTICAL
, weight = 1
)
EditText
(width = MATCH_PARENT
)LinearLayout
(第3页,orientation = VERTICAL
, weight = 1
)
EditText
(width = MATCH_PARENT
)注意:我没有测试过。
答案 2 :(得分:0)
对于EditTexts
,当您将layout_width
或height
设置为match_parent
时,他们将匹配其父级设置的任何大小。但是,一旦内容填充文字,它们的尺寸就会扩大,因此宽度/高度与ScrollView
相同。
答案 3 :(得分:0)
虽然可以使用水平滚动视图完成。我发现使用视图寻呼机要容易得多。它也更清洁,我认为可以更顺畅地滑动。
添加xml
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="70dp"/>
设置寻呼机
mPager =(ViewPager)getActivity().findViewById(R.id.pager);
mPager .setAdapter(new CustomPagerAdapter());
mPager .setOffscreenPageLimit(6);
适配器
public class CustomPagerAdapter extends PagerAdapter {
@Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) container.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View page= inflater.inflate(R.layout.page_item, container, false);
TextView textView =(TextView)page.findViewById(R.id.textView);
textView.setText("Text");
container.addView(page);
return(page);
}
@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
container.removeView((View)object);
}
@Override
public int getCount() {
return 16;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals( object );
}
}
答案 4 :(得分:0)
我发现一些边缘情况,其中rupps发布的FullWidthEditText
没有用,所以我修改它以适用于我发现的所有情况:
public class FullWidthTextView extends android.support.v7.widget.AppCompatTextView
{
public FullWidthTextView( final Context context )
{
super( context );
}
public FullWidthTextView( final Context context, @Nullable final AttributeSet attrs )
{
super( context, attrs );
}
public FullWidthTextView( final Context context, @Nullable final AttributeSet attrs, final int defStyleAttr )
{
super( context, attrs, defStyleAttr );
}
@Override
protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec )
{
View parentScrollView = ((View) (getParent().getParent()));
boolean needsRemeassure = false;
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
int parentWidth = parentScrollView.getMeasuredWidth();
if( parentWidth > 0 )
{
widthMeasureSpec = parentWidth;
}
// It's possible for the parent to still have ZERO width
// in this case we must request to be remeassured
else
{
needsRemeassure = true;
}
}
}
super.onMeasure( widthMeasureSpec, heightMeasureSpec );
setMeasuredDimension( View.MeasureSpec.makeMeasureSpec( widthMeasureSpec, MeasureSpec.EXACTLY ),
View.MeasureSpec.makeMeasureSpec( getMeasuredHeight(), MeasureSpec.EXACTLY ) );
if( needsRemeassure )
{
post( new Runnable()
{
@Override
public void run()
{
requestLayout();
}
} );
}
}
}