繁体   English   中英

Android如何将视图的宽度设置为屏幕宽度的一定比例?

[英]Android how to set the width of a view to a certain ratio of the width of the screen?

我创建3个水平放置在LinearLayout TextViews 现在,我希望它们平均分别占据屏幕宽度的1/3。 换句话说,任何TextView的宽度都是由屏幕的宽度决定的,该比率始终为1/3。

我想知道是否有任何方法可以仅通过修改xml文件来实现此目标?

main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView1" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView2" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView3" />

</LinearLayout>

尝试这个:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:text="TextView1"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:text="TextView2"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:text="TextView3"
        android:layout_weight="1" />

</LinearLayout>

尝试这个:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="3" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView1" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView2" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView3" />

</LinearLayout>

您应该以编程方式执行此操作:

TypedValue tv = new TypedValue();
TypedValue.complexToDimensionPixelSize(tv.data,activity.getResources().getDisplayMetrics());

或者这样:

DisplayMetrics displayMetrics = activity.getResources().getDisplayMetrics();
int px = Math.round(dp * (displayMetrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT));

应用重量android:layout_weight="?" xml文件中的每个视图。

在textview上添加额外的线性布局。 设置线性布局的宽度,例如android:layout_width = "0px" ,并将文本通过设置为android:layout_width="full_parent"

别忘了在父线性布局上进行设置,以在子项中设置weigthsum和layout_weigth

您只需要在每个textview上添加一个代码行

机器人:layout_weight = “0.3”

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView1"
    android:gravity="center_horizontal"
    android:layout_weight="0.3"/>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView2"
    android:gravity="center_horizontal"
    android:layout_weight="0.3"/>

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView3"
    android:gravity="center_horizontal"
    android:layout_weight="0.3"/>

</LinearLayout>

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM