Android 相对于另一个文本视图空间展开一个文本视图空间
在给定的代码中,我在水平视图中有两个文本视图。 我希望当一个文本视图的文本进入另一行时,另一个文本视图的空间也应该根据第一行增加Android 相对于另一个文本视图空间展开一个文本视图空间,android,textview,Android,Textview,在给定的代码中,我在水平视图中有两个文本视图。 我希望当一个文本视图的文本进入另一行时,另一个文本视图的空间也应该根据第一行增加 正如在给定的图像中一样,段落可以有3-4行,但我希望名称文本视图应该垂直展开。试试这个: 只需给匹配父母的身高 如果希望文本居中,可以在两个文本视图中添加android:gravity=“center\u vertical”。试试这个 <LinearLayout xmlns:android="http://schemas.android.com/apk/
正如在给定的图像中一样,段落可以有3-4行,但我希望名称文本视图应该垂直展开。试试这个: 只需给匹配父母的身高
如果希望文本居中,可以在两个文本视图中添加android:gravity=“center\u vertical”
。试试这个
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#c91799c9"
android:text="Name."
android:textColor="#000"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#3f1799c9"
android:paddingLeft="8dp"
android:text="paragraph"
android:textColor="#000"
android:textSize="17sp"
android:textStyle="bold" />
</LinearLayout>
输出
试试这个,
在两个textView
中设置android:layout\u height=“match\u parent”
,也可以在两个textView中设置相同的Textsize
android:Textsize=“24sp”
将重心放在第一个文本视图上。即在第一个文本视图“名称”中添加以下代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#c91799c9"
android:text="Name."
android:textColor="#000"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#3f1799c9"
android:paddingLeft="8dp"
android:text="Paragraph"
android:textColor="#000"
android:textSize="17sp"
android:textStyle="bold" />
</LinearLayout>
还可以更改两个TextView的高度以匹配父项
android:layout_gravity="center"
您必须在显示布局后设置宽度,否则您将获得
错误,有一个命令.onPost可用于避免此错误
错误
在我的示例中,使用my\u image.getWidth()
并将此值设置为其他组件,如bt_start或et_user but
您可以在任何其他组件中执行此操作
可以更改任何其他布尔比较,如:
这只是为了确认从中获取宽度的项目不为空,因此
您可以正确获取宽度,如果此项为空,您将获得一个
空点异常
检查我的答案。如果这解决了您的问题,那么请向上投票回答:)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#c91799c9"
android:text="Name."
android:textColor="#000"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#3f1799c9"
android:paddingLeft="8dp"
android:text="Paragraph"
android:textColor="#000"
android:textSize="17sp"
android:textStyle="bold" />
</LinearLayout>
android:layout_gravity="center"
android:layout_height="match_parent"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:textSize="24sp"
android:text="Name"
android:textColor="#000"
android:textStyle="bold"
android:gravity="center"
android:background="#c91799c9"
android:layout_height="match_parent" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:textSize="17sp"
android:paddingLeft="8dp"
android:text="Paragraph"
android:gravity="center"
android:textColor="#000"
android:textStyle="bold"
android:background="#3f1799c9"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ImageView
android:id="@+id/my_image"
android:layout_width="wrap_content"
android:layout_height="50dp"/>
<EditText
android:id="@+id/et_user"
android:layout_width="wrap_content"
android:layout_height="50dp"/>
<Button
android:id="@+id/bt_start"
android:layout_width="wrap_content"
android:layout_height="50dp"/>
</LinearLayout>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView my_image = findViewById(R.id.my_image);
Button bt_start = findViewById(R.id.bt_start);
EditText et_user = = findViewById(R.id.et_user);
if ((my_image != null))
{
my_image.post(new Runnable()
{
@Override
public void run()
{
bt_start.setWidth(my_image.getWidth());
et_user.setWidth(my_image.getWidth());
}
});
}
my_image != null
bt_start != null
et_user != null