问题
i am trying to align multiple (variable) textviews in one line. It should look like this:
- Textview1 | Textview2 | TextView3
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).
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>
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!).
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.
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 ?
Thank you
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>
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).
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) without "singleline=true"

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:

回答1:
Change the relative layout to a linear layout and then set the orientation to horizontal something like this.
<LinearLayout:id="@+id/persondetails_names_horizontal_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" />
<TextView
android:id="@+id/persondetails_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="3dp"
android:text=" Title "
/>
<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"
/>
<TextView
android:id="@+id/persondetails_surname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Surname " />
</LinearLayout>
回答2:
You should try and do a LinearLayout and weight each element.
<LinearLayout
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_weight= "1"
android:layout_centerVertical="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_weight= "1"
android:layout_centerVertical="true"/>
<TextView
android:id="@+id/persondetails_surname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight= "1"
android:layout_centerVertical="true"
android:text=" Surname " />
</LinearLayout>
回答3:
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?
To answer you
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"
EDIT
this is what i mean
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
来源:https://stackoverflow.com/questions/30290626/android-align-multiple-textview-s-in-line