Android的对齐多个TextView的-S一致 [英] Android align multiple TextView-s in line
问题描述
我试图调整多个(变量)textviews在一行。它应该是这样的:
i am trying to align multiple (variable) textviews in one line. It should look like this:
- Textview1 | Textview2 | TextView3
由于textviews变化就长我有一个问题,正确地对准他们(我唯一知道的是,Textview1将是最短的一个)。
Since the textviews vary regarding length I am having a problem aligning them properly(the only thing I know is that Textview1 will be the shortest one).
下面是我的code(它被放置在一个线性布局):
Here is my code (it is placed inside a Linear Layout):
<RelativeLayout
android:id="@+id/persondetails_names_horizontal_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right" >
<TextView
android:id="@+id/persondetails_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:paddingTop="3dp"
android:text=" Title "
android:layout_alignParentRight="true" />
<TextView
android:id="@+id/persondetails_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Name "
android:textSize="@dimen/font_big"
android:textStyle="bold"
android:layout_toEndOf="@id/persondetails_title"
android:layout_toRightOf="@id/persondetails_title" />
<TextView
android:id="@+id/persondetails_surname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/persondetails_name"
android:layout_toRightOf="@id/persondetails_name"
android:text=" Surname " />
</RelativeLayout>
现在,只要这3 Textviews填写一行在屏幕上一切正常,但只要其中一人变得越来越大,我得到了一些问题。例如,当我把姓
字段很长,它推动了其他2次在屏幕的(它们是不可见),并采取所有的空间(但只有一个行了!)。
Now, as long as these 3 Textviews fill out one line on the screen everything is OK, but as soon as one of them gets bigger I get some problems. For example, when I put the surname
field very long, it pushes the other 2 views out of the screen (they are not visible) and takes all the space (but just one line!).
我要的是,这些观点是一致的右侧,他们每个人的previous一个正确的,当它是需要断行到一个新行(无马瑟这TextView的是)而在新线以下textviews应对准它的权利。
What I want is that these views are aligned to the right side, each of them to the right of the previous one, and when it is needed to linebreak into a new line (no mather which textview it is) and in the new line the following textviews should align to the right of it.
那么,我需要改变我的code,使这些Textviews对齐彼此相邻,并妥善转移时,其中一个挺大的?
So what do I need to change in my code, so that these Textviews are aligned next to each other, and moved properly when one of them is getting bigger ?
感谢您
这是我是如何实现Elltz建议:
This is how I implemented Elltz suggestions:
<LinearLayout
android:id="@+id/persondetails_namen_horizontal_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:orientation="horizontal"
android:weightSum="2" >
<TextView
android:id="@+id/persondetails_wert_titel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right" />
<TextView
android:id="@+id/persondetails_wert_vorname"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_below="@id/persondetails_text_vorname"
android:gravity="right"
android:layout_gravity="right"
android:text=" "
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:overScrollMode="always"
android:scrollHorizontally="true"
android:singleLine="true"/>
<TextView
android:id="@+id/persondetails_wert_nachname"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_below="@id/persondetails_text_vorname"
android:layout_gravity="right"
android:text=" "
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:overScrollMode="always"
android:scrollHorizontally="true"
android:singleLine="true"/>
</LinearLayout>
但仍然没有工作。其实我去掉了单线=真,因为情况下的3 textviews一个得到太久,我需要它打破(不要有破有问题)。
But still not working. Actually I removed the "singleLine=true" because in case when one of the 3 textviews gets too long I need it break (don't have a problem with breaking).
对于破,我想因为它需要每一个TextView中采取的MUCHE空间,唯一的事情是,所有其他视图768,16正确地移动。
Regarding breaking, I want every textview to take as muche space as it needs, the only thing is that all the other views shoud be moved properly.
属性单行不可能是正确的,因为它可能是其中一个视图变得太长一条线,但我已经有单线,没有测试过它:
The property "single line" cannot be right because it is possible that one of these views gets too long for one line, but I have tested it with single line and without:
一)以单线=真
a) with "singleline=true"
b)无单线=真
b) without "singleline=true"
在博特情况下,我得到一个错误的输出,但情况2更接近我想要的。但是,如果你看它,你会看到textview2被推并breakes在其边界而不是对整个lenght。 TextView的3采取了大部分空间。但我想是每一个观点是完全streched,如果需要breaked到新的生产线(textview1是这样的弹匣。)
In bothe cases I get a wrong output, but case 2 is closer to what I want. But if you look at it you will see that textview2 is being pushed and it breakes in its bounds but not on the entire lenght. Textview 3 takes the most space. But what i want is that every view is fully streched and if needed breaked into new line (textview1 is this "Mag.")
这是我怎么想它看起来像:
This is how I would like it to look like:
推荐答案
要回答为什么你的code线是给你的问题是 RelativeLayout的
定位及其查看
的亲戚给对方,一个查看
将需要另外一个查看
进行奠定了如果不是父母本身。因此,的TextView
1的定位是左的TextView
2时,的TextView
2变大,因为它的左边将是向左而不是在屏幕上的布局和空间得到它?
To answer why your code line is giving you that problems is a RelativeLayout
positions its View
s relative to each other, a View
will need another View
to be layed out if not the parent itself. So TextView
1 is positioned to the left of TextView
2 when TextView
two gets bigger since its to the left it will be to the left but not on screen layout or space get it?
为您解答
的TextView
1将在最短的使用与的LinearLayout
办法 weightsum
和重量
调整这种方式首先设置 weightsum
两对的LinearLayout
和两我要很长的的TextView
取值给他们的宽度0dp
和体重 1
每个最短的TextView
将 WRAP_CONTENT
为宽度
,这就是一切生命的还是不错的。现在两个长的TextView
旨意计算它们的大小和剩余部分将留待较短。我不的不明确你希望你的的TextView
来长的水平直线的角度,但你做了之后,你可以添加这些元素到你的的TextView
在XML或者你可以做到这一点在Java中,以确保您的正直状的TextView
取值
TextView
1 will be the shortest Using the LinearLayout
Approach with weightsum
and weight
tweak it this way first set the weightsum
to two on the LinearLayout
and the two i am going to be long TextView
s give them a width of 0dp
and weight 1
each the shortest TextView
will have wrap_content
as width
and that's all life's is good. now the two long TextView
s will calculate their sizes and the rest will be left for the shorter one. I do not not specifically how you want your TextView
to long in terms of straight horizontal line but after you have done that you can add these elements to your TextView
in xml or you could do it in java to ensure your straight lined TextView
s
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:overScrollMode="always"
android:scrollHorizontally="true"
android:singleLine="true"
修改
这是我的意思
int width = getWindow().getDecorView().getMeasuredWidth();
//i am getting the overal width of the screen
Toast.makeText(getApplicationContext(), String.valueOf(width), Toast.LENGTH_SHORT).show();//toast it
for(int i =0; i < ((LinearLayout)findViewById(R.id.l)).getChildCount(); i++){
//looping through the parent container
TextView child = (TextView) ((LinearLayout)findViewById(R.id.l)).getChildAt(i);
child.setSelected(true);
child.setEllipsize(TruncateAt.MARQUEE);
child.setHorizontallyScrolling(true);
child.setSingleLine(true);
if(i+1 == 3){ //the last textview
child.setLayoutParams(new LinearLayout.LayoutParams(
((LinearLayout)findViewById(R.id.l)).getChildAt(1).getRight(),
LinearLayout.LayoutParams.WRAP_CONTENT));
}else{ // the first two take 1/3 of the width of the screen
child.setLayoutParams(new LinearLayout.LayoutParams(width/3, LinearLayout.LayoutParams.WRAP_CONTENT));
}
}
}
希望它可以帮助
Hope it helps
这篇关于Android的对齐多个TextView的-S一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!