相对于另一个扩展一个文本视图 space
Expand one textview space with respect to another
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:textSize="24sp"
android:text="Name."
android:textColor="#000"
android:textStyle="bold"
android:background="#c91799c9"
android:layout_height="wrap_content" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:textSize="17sp"
android:paddingLeft="8dp"
android:text="Paragraph"
android:textColor="#000"
android:textStyle="bold"
android:background="#3f1799c9"
android:layout_height="wrap_content" />
</LinearLayout>
在给定的代码中,我在水平视图中有 2 个文本视图。
我希望当一个 textview 的文本进入另一行时,另一个 textview space 也应该根据第一个
增加
给定图片中的段落可以是 3-4 行,但我希望 Name textview 应该垂直展开
试试这个:
Just give height to match_parent
<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>
如果你想让文本居中,你可以在两个文本视图中添加 android:gravity="center_vertical"
。
试试这个
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:textSize="24sp"
android:text="Name."
android:textColor="#000"
android:textStyle="bold"
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:textColor="#000"
android:textStyle="bold"
android:background="#3f1799c9"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
输出
试试这个,
在 textView
中设置 android:layout_height="match_parent"
也可以在 textView
android:textSize="24sp"
中设置相同的 Textsize
<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.ie 将下面的代码添加到第一个 TextView 'Name'。
android:layout_gravity="center"
并且还将两个 TextView 的高度更改为 match_parent。
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>
You have to set the width after the layout is shown or you will get an
error, there is a command .onPost that you can use to avoid this
error.
<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>
the java code must be places in OnCreate method but after
setContentView
@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());
}
});
}
In my example you get the imageview width using my_image.getWidth()
and set this value to other components like bt_start or et_user but
you can do it in any other component.
my_image != null
can be change for any other boolean comparision like :
bt_start != null
et_user != null
this is only to confirm the item you get the width from is not null so
you could get the width correctly, if this item was null you get a
null point exception.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:textSize="24sp"
android:text="Name."
android:textColor="#000"
android:textStyle="bold"
android:background="#c91799c9"
android:layout_height="wrap_content" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:textSize="17sp"
android:paddingLeft="8dp"
android:text="Paragraph"
android:textColor="#000"
android:textStyle="bold"
android:background="#3f1799c9"
android:layout_height="wrap_content" />
</LinearLayout>
在给定的代码中,我在水平视图中有 2 个文本视图。 我希望当一个 textview 的文本进入另一行时,另一个 textview space 也应该根据第一个
增加给定图片中的段落可以是 3-4 行,但我希望 Name textview 应该垂直展开
试试这个:
Just give height to match_parent
<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>
如果你想让文本居中,你可以在两个文本视图中添加 android:gravity="center_vertical"
。
试试这个
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:textSize="24sp"
android:text="Name."
android:textColor="#000"
android:textStyle="bold"
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:textColor="#000"
android:textStyle="bold"
android:background="#3f1799c9"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
输出
试试这个,
在 textView
中设置 android:layout_height="match_parent"
也可以在 textView
android:textSize="24sp"
Textsize
<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.ie 将下面的代码添加到第一个 TextView 'Name'。
android:layout_gravity="center"
并且还将两个 TextView 的高度更改为 match_parent。
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>
You have to set the width after the layout is shown or you will get an error, there is a command .onPost that you can use to avoid this error.
<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>
the java code must be places in OnCreate method but after setContentView
@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());
}
});
}
In my example you get the imageview width using
my_image.getWidth()
and set this value to other components like bt_start or et_user but you can do it in any other component.
my_image != null
can be change for any other boolean comparision like :
bt_start != null
et_user != null
this is only to confirm the item you get the width from is not null so you could get the width correctly, if this item was null you get a null point exception.