标签: android-layout-weight

如何设置textview width wrap_content但限制为父宽度的1/3

有一个TextView,它的宽度不应超过其父宽度的1/3.如果它的宽度小于父级的1/3,它应该有wrap_content行为.它的横向兄弟将始终在它旁边开始.

尝试下面,它总是有1/3和2/3的硬切,所以如果text1的空间小于1/3,则TextView两个不会在它旁边开始.

改变LinearLayoutRelativeLayout,然后android:layout_weight="n"不起作用

基本上,需要定义宽度是wrap_contentmaxWidth不超过1/3.

有什么建议吗?

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="3">

    <TextView 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:singleLine="true"
        android:ellipsize="end"
    />

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:singleLine="true"
        android:ellipsize="end"
        android:layout_weight="2"
    />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

android textview android-layout-weight

10
推荐指数
1
解决办法
8842
查看次数

TableLayout权重和问题

我正试图在我的TableLayout中实现40/60%的分割,但我没有运气.在我的Android设备上测试代码时,左侧TextView仍然占用了60%,而不是40%.

任何帮助都会很棒:

    <TableLayout

    android:id="@+id/agentStatusTable"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_gravity="center|top"
    android:visibility="visible"
    android:background="@drawable/curvedbg"
    android:layout_marginLeft="10sp"
    android:layout_marginRight="10sp"
>

<TableRow android:id="@+id/webiprow" android:layout_marginTop="14sp" android:gravity="center_vertical" android:background="#e5e6e6" android:minHeight="45sp" android:weightSum="1">
<TextView android:paddingRight="10dp" android:id="@+id/webiptitle" android:text="@string/webip" android:textColor="#000000" android:textSize="15sp" android:layout_weight="0.40" android:gravity="right"></TextView> 
<TextView android:paddingRight="10dp" android:id="@+id/webiptext" android:textColor="#000000" android:textSize="15sp" android:layout_weight="0.60" android:gravity="right"></TextView> 
</TableRow>

<TableRow android:id="@+id/webportrow" android:layout_marginTop="6dp" android:layout_marginBottom="14sp"  android:gravity="center_vertical" android:minHeight="45sp" android:weightSum="1">
<TextView android:paddingRight="10dp" android:id="@+id/webporttitle"  android:layout_weight="0.40"  android:text="@string/webport" android:textColor="#000000" android:textSize="15sp" android:gravity="right"></TextView> 
<EditText android:paddingRight="10dp" android:inputType="number"  android:layout_weight="0.60"  android:id="@+id/EditText01" android:text="@+id/EditText01"></EditText></TableRow>

</TableLayout>
Run Code Online (Sandbox Code Playgroud)

android tablerow android-layout android-tablelayout android-layout-weight

9
推荐指数
1
解决办法
1万
查看次数

如何在使用setRotation后刷新linearLayout中的match_parent?

我有几个嵌套的布局,我试图在代码中按需旋转90度.我已经把setRotation的一部分工作得很好,但不幸的是,事情并没有通过轮换来调整.这些元素的宽度设置为match_parent,旋转后它仍然匹配父宽度,而不是它应匹配的父高度.

XML

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="link.basiclifecounter.LifeCounter"
    android:background="#CC00CC">

    <LinearLayout
        android:id="@+id/topPlayers"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:orientation="vertical"
        android:background="#CC0000">

        <RelativeLayout
            android:id="@+id/p3"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

        **A bunch of stuff in here**

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/p2"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

        **A bunch of stuff in here**

        </RelativeLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/bottomPlayer"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:background="#00CC00">

        <RelativeLayout
            android:id="@+id/p1"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

        **A bunch of stuff in here**

        </RelativeLayout>
    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

旋转Java代码

view.findViewById(R.id.topPlayers).setRotation(90); //Rotate the entire top box
view.findViewById(R.id.p3).setRotation(180); //Flip one side so both …
Run Code Online (Sandbox Code Playgroud)

android rotation android-layout android-xml android-layout-weight

9
推荐指数
1
解决办法
1205
查看次数

如何为包含的布局设置重量?

我有两个xml文件,页眉和页脚,如下所示

header.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="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#9d1246" >

        <Button
            android:id="@+id/button_backfromreg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/backbtn"
            android:visibility="visible" />
    </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

footer.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="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#9d1246" >
    </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我在mainlayout中包含了上面两个布局,如图所示

<?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:background="#eaedf2"
    android:orientation="vertical" >

    <include
        android:layout_weight="1"
        layout="@layout/header" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="30dp"
        android:layout_weight="1"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:editable="false"
            android:ems="10"
            android:hint="Country"
            android:textColor="#655e5e"
            android:textSize="15sp" >

            <requestFocus />
        </EditText> …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-layout-weight

8
推荐指数
1
解决办法
5140
查看次数

layout_weight与它应该的相反

在下面的xml中,我试图绘制一个包含两个块的布局(LinearLayout和TextView).我希望LinearLayout比TextView大5倍.这个xml与我预期的完全相反,TextView占用的空间比LinearLayout多5倍.请注意,我已将两个元素的宽度设置为0dp,这是一个常见的疏忽.

<?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:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:layout_gravity="center_vertical"
    android:weightSum="6"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="5" >

        <TextView
            android:id="@+id/result_title_textview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="Arial"
            android:textColor="#222222"
            android:textSize="14sp"/>

        <TextView
            android:id="@+id/result_info_textview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="Arial"
            android:textColor="#777777"
            android:textSize="12sp" />
    </LinearLayout>

    <TextView
        android:id="@+id/result_distance_textview"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:fontFamily="Arial"
        android:textColor="#ffffffff"
        android:textSize="14sp" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

编辑:此布局实际上是列表项,包含在此列表中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/results_list_fragment_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingTop="15dp" >

    <ListView
        android:id="@+id/search_results_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/scrollable_content_background"
        android:divider="@drawable/listview_divider"
        android:dividerHeight="1dp" >
    </ListView>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

android android-linearlayout android-layout-weight

8
推荐指数
2
解决办法
1195
查看次数

Android:以编程方式设置权重参数与我想要的相反

我遇到了android的问题.我在Java中设置了Weight参数,但它正在完成我想要的对话.

这是代码

LinearLayout container = new LinearLayout(context);
// some code ...
container.setWeightSum(1f);

View v1 = new View(context);
v1.setBackgroundColor(Color.parseColor("#ff0000"));
LinearLayout.LayoutParams p1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
p1.weight=0.1f;

View v2 = new View(context);
v2.setBackgroundColor(Color.parseColor("#000000"));
LinearLayout.LayoutParams p2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
p2.weight=0.9f;

container.addView(v1,p1);
container.addView(v2,p2);
Run Code Online (Sandbox Code Playgroud)

我在容器布局之间添加了一条黑线,重复了这个过程7次.通常情况下,我应该在lef上获得一个小的红色列,以及一个大的黑色列,但这是我得到的代码:

http://i.stack.imgur.com/PPgoy.png

为什么它与代码完全相反?

谢谢你的帮助.

java android android-layout android-layout-weight

7
推荐指数
1
解决办法
9076
查看次数

7
推荐指数
1
解决办法
2619
查看次数

如何通过焦点在屏幕上调整两个片段的大小?

我有三个片段,前两个填充80%的屏幕,最后一个填充其余的片段(这个片段永远不会改变大小).我希望,在用户(焦点)输入片段后,调整片段的大小,使其填满屏幕的70%(将10%留给另一个).像这样:

在此输入图像描述

可以通过动态改变碎片的重量吗?或者有更好的方法来实现这一目标?

这是我现在在XML中的代码:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:weightSum="1.0">

            <FrameLayout android:id="@+id/containerParent"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight=".8">

                <LinearLayout
                    android:id="@+id/MainLinear"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical"
                    android:weightSum="1.0">

                    <FrameLayout android:id="@+id/fragment1"
                        android:layout_width="fill_parent"
                        android:layout_height="0dp"
                        android:layout_weight=".5"/>

                    <FrameLayout android:id="@+id/fragment2"
                        android:layout_width="fill_parent"
                        android:layout_height="0dp"
                        android:layout_weight=".5"/>

                </LinearLayout>

            </FrameLayout>


            <FrameLayout android:id="@+id/fragment3"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight=".2"/>


        </LinearLayout>

    </FrameLayout>


</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

android android-fragments android-layout-weight

7
推荐指数
1
解决办法
1371
查看次数

Android LinearLayout不可见

我正在开发一个android应用程序。

我有一个问题,我的LinearLayout窗口小部件在屏幕上不可见。

这是我的布局

<LinearLayout
    android:id="@+id/linear_layout_row_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:layout_marginStart="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginEnd="5dp"
    android:layout_marginRight="5dp"
    android:orientation="vertical"
    android:weightSum="2">

    <LinearLayout
        android:id="@+id/linear_layout_row1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="60dp"
        android:layout_marginEnd="60dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="2">

        // Other widgets

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linear_layout_row2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="60dp"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="60dp"
        android:layout_marginBottom="40dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="1">

        // Other widgets

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linear_layout_row3"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="60dp"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="60dp"
        android:layout_marginBottom="40dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="2">

        // Other widgets
    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

在这种情况下linear_layout_row3,屏幕上不可见。

如果我将其向上移动,则会显示出来,但是下面移动的布局将不可见。

我不明白这个问题,您能帮忙找到它吗?

android android-layout android-layout-weight

7
推荐指数
0
解决办法
63
查看次数

在 Jetpack compose 中定义视图权重时出现问题

我想使用权重制作一个简单的喷气背包组合布局。下面是代码

Row() {
Column(
    Modifier.weight(1f).background(Blue)){
    Text(text = "Weight = 1", color = Color.White)
}
Column(
    Modifier.weight(2f).background(Yellow)
) {
    Text(text = "Weight = 2")
}
}
Run Code Online (Sandbox Code Playgroud)

这将导致这样的布局 使用权重的简单行列布局。

但是如果我的内部视图来自其他一些可组合函数,而该函数不知道它将成为列或行的子级,该怎么办?

Row() {
  // Calling Some composable function here
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我无法访问 Modifier.weight(..) 函数,因为它认为它不在行或列范围内,因为它是一个独立的函数。

android android-layout-weight android-jetpack-compose

7
推荐指数
2
解决办法
4378
查看次数