我在同一行中对齐了两个TextViews
。如果TextViews
中的文本都很小,则它们适合屏幕中的同一行。但是,如果左侧TextView
(employee name
)中的文字增加,则右侧TextView
(employee type
)中的文字会转移到第二行,但会转移到右侧的屏幕。如果左TextView
中的employee name
增加且employee name
TextView
,则如何使第二个TextViews
与左侧和第二行(<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:id="@+id/employee_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22.0sp" />
<TextView android:id="@+id/employee_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:layout_toRightOf="@id/employee_name"
android:layout_alignBaseline="@id/employee_name"
android:textSize="15.0sp" />
</RelativeLayout>
下方)对齐}不适合同一行?
请查看以下截图:
请在下面找到我的代码:
{{1}}
答案 0 :(得分:1)
我建议使用以下方法之一:
答案 1 :(得分:0)
试试这个:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:weightSum="2"
android:orientation="horizontal"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:id="@+id/employee_name"
android:layout_width="match_parent"
android:weightSum="2"
android:weight="1"
android:layout_height="wrap_content"
android:textSize="22.0sp" />
<TextView android:id="@+id/employee_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weight="1"
android:paddingLeft="8dp"
android:textSize="15.0sp" />
</LinearLayout>
答案 2 :(得分:0)
答案是使用单个TextView
,并使用Spans修改特征。
假设您只有TextView
@+id/employee_name
:
TextView employeeInfo = (TextView) findViewById(R.id.employee_name);
String employeeName = // get your employee name
String employeeType = // get your employee type
// Assuming you move your 15sp into a dimens.xml resource file
int employeeTypeSize = getResources().getDimensionPixelSize(R.dimen.employee_type_text_size);
SpannableStringBuilder builder = new SpannableStringBuilder(employeeName);
builder.append(employeeType);
// Set the smaller text size from the end of the name to the end
// of the string.
builder.setSpan(new AbsoluteSizeSpan(employeeTypeSize),
employeeName.length(), builder.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// Then set this spannable to the TextView
employeeInfo.setText(builder);