Android 用文本视图包装布局
我的布局如下所示:Android 用文本视图包装布局,android,layout,Android,Layout,我的布局如下所示: <RelativeLayout> <!--aligned parent top--> </<RelativeLayout> <FlowLayout> <!--Should fill remaining space--> </FlowLayout> <RelativeLayout> <!--aligned parent bottom--> </<RelativeLa
<RelativeLayout>
<!--aligned parent top-->
</<RelativeLayout>
<FlowLayout>
<!--Should fill remaining space-->
</FlowLayout>
<RelativeLayout>
<!--aligned parent bottom-->
</<RelativeLayout>
<LinearLayout android:orientation="vertical" ... >
<RelativeLayout ... />
<FlowLayout android:layout_height="0dp" android:layout_weight="1" ... />
<RelativeLayout ... />
</LinearLayout>
尝试以编程方式将textView添加到for循环中的linearLayout中,在该循环中,添加每个textView后,获得可用的剩余宽度(可以使用[ScreenWidth-(添加的textView宽度之和+[textView数量*每个textView的边距/填充]])*)
如果宽度小于某个最小值(假设每个textview的最小值为100px),则中断循环并添加计数指示器文本
我可以想到下面的代码,它可能不精确,但只是告诉你我在想什么
int screenWidth = getScreenWidth();
int currentWidth=0;
int minimumWidthForAChildWithOffset = 100;//offset should include width of indicator
int childCount = 10;
int paddingForEachChild = 15;
for (int i = 0; i <childCount; i++) {
TextView label= getChildView();
parent.addView(label, i);
currentWidth += parent.getChildAt(i).getWidth() + paddingForEachChild;
if(i!=childCount-1/*ignore if this is the last item*/ && screenWidth-currentWidth<=minimumWidthForAChildWithOffset){
//add indicator label to layout here
break;
}
}
int screenWidth=getScreenWidth();
int currentWidth=0;
偏移量为100的子对象的int最小宽度//偏移量应包括指示器的宽度
int childCount=10;
int paddingForEachChild=15;
对于(int i=0;i尝试在循环中添加TextView,在新的OnPreDrawListener中分别添加每个TextView。添加每个TextView后,检查它是否超出行上的空间,如果超出,请删除它并添加计数器。如果它仍然超出行,请再删除一个并增加计数器
关键是在一个单独的OnPreDrawListener中测试每一个,所以这一切都发生在视图显示之前,但是在视图计算了布局和大小之后。如果我必须实现您描述的东西,我会尝试分叉/子类化FlowLayout,并更新其onMeasure
和onLayout
方法
以下是流程布局:
1) 第一步是在测量中限制FlowLayout的高度。此外,这样做可能就足够了:
<RelativeLayout>
<!--aligned parent top-->
</<RelativeLayout>
<FlowLayout>
<!--Should fill remaining space-->
</FlowLayout>
<RelativeLayout>
<!--aligned parent bottom-->
</<RelativeLayout>
<LinearLayout android:orientation="vertical" ... >
<RelativeLayout ... />
<FlowLayout android:layout_height="0dp" android:layout_weight="1" ... />
<RelativeLayout ... />
</LinearLayout>
2) 第二步是更新onLayout
,当达到最大高度时,它停止添加线条和线条视图,然后添加显示隐藏视图数量的视图您能否获得FlowLayout中可见的文本视图数量?否则,您将放入scrollview。所以你不必计算。我不想滚动流布局。正如您在上面的屏幕截图中所看到的,我希望显示尽可能多的文本视图,并在最后显示“+剩余”。如果屏幕较大,我希望显示所有5个文本视图,而不是屏幕截图中的“+3”。这显然需要自定义布局。测量可用视图,尝试获取尽可能多的视图以适应它,并为其余视图显示溢出视图。仅仅使用线性布局或类似的布局,你可能不会得到任何好的结果。。。Textview的宽度不是固定的。并非所有文本的长度都相同。文本来自REST服务。那么,如果有三个文本长度为2-3个字符,那么第四个文本长度为20个字符?