标签: android-layout-weight

线性布局 - 重量和FILL_PARENT之间的差异

根据文档,FILL_PARENT基本上让视图占用了整个额外的空间.重量还决定了视图可以占用多少空间.有什么不同?

例如:我使用时会发生什么,

new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f)
Run Code Online (Sandbox Code Playgroud)

android params android-layout android-linearlayout android-layout-weight

4
推荐指数
1
解决办法
5573
查看次数

Android布局 - 两个视图彼此相邻,宽度相等,使用全屏宽度

我在android中的布局有问题.我希望两个视图应该具有相同的宽度,并且它们应该几乎使用屏幕的整个宽度.每个视图都应包含一个居中的标签.

它完成时应该看起来像这样:

例

这是我到目前为止:

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

        <View
            android:id="@+id/view1"
            android:layout_width="10dp"
            android:layout_height="90dp"
            android:layout_alignParentTop="true"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp" />

        <View
            android:id="@+id/view2"
            android:layout_width="10dp"
            android:layout_height="90dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginTop="10dp"
            android:layout_marginRight="10dp" />

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

我现在只有宽度的占位符值.

谢谢.

android view android-layout android-layout-weight

4
推荐指数
1
解决办法
3504
查看次数

在 Android 中使用布局权重属性动态添加视图

我需要将视图动态添加到我LinearLayout的 xml 文件中已有的视图中。我尝试添加布局并能够添加它,但问题是我无法正确将布局权重属性设置为自定义添加的视图。它总是有问题。

这是我的 XML(我期待的视图)

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

    <TextView
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_weight="0.1"
        android:text="text 1" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_weight="0.6"
        android:text="text 2" />

    <CheckBox
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_weight="0.3"
        android:text="Check Box 1" />

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

他是我的 Java 代码,我将视图动态添加到布局中

public class MyActivity extends Activity {

    private ViewGroup mLinearLayout;  // this should be your main layout where your planning to add the views programatically 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout1);
        mLinearLayout = (ViewGroup) findViewById(R.id.linear_layout);
        addLayout("text …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-linearlayout android-layout-weight

4
推荐指数
1
解决办法
2799
查看次数

Android 布局权重%

我正在尝试摆弄 Android 布局权重属性。我想要一个具有“3 个子布局”的垂直布局,第一个将占用 25% 的空间,第二个将占用 50%,最后一个将占用 25% 的空间。当我尝试添加到最后一个布局时,一切都不起作用。每个布局的权重应该是多少?

我只使用 2 个布局/元素就可以使权重属性正常工作,并了解它是如何工作的,但是当我尝试使用 3 个布局/元素时,我遇到了问题。

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:orientation="horizontal"
    android:weightSum="100" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:orientation="vertical"
        android:layout_weight="25" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:orientation="vertical"
       android:layout_weight="50" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:orientation="vertical"
        android:layout_weight="25" >
    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

android android-layout android-layout-weight

3
推荐指数
1
解决办法
4312
查看次数

为什么相同的layout_weight导致Android ImageButton和Button的宽度不同?

当我尝试将常规按钮和ImageButton设置为相同时,我在LinearLayout中得到了一些奇怪的行为layout_weight.图像按钮的android:src资源非常小,非常适合ImageButton,没有任何问题.即使它们的宽度应该相同,但由于某种原因,ImageButton的宽度大约是普通按钮其余部分的2/3.如果我将ImageButton的乘以layout_weight10,它更接近正确的大小,但为什么这不起作用?

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="1">
    <Button
            android:id="@+id/clear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/clear"/>
    <Button
            android:id="@+id/left_paren"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/left_paren"/>
    <Button
            android:id="@+id/right_paren"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/right_paren"/>
    <ImageButton
            android:id="@+id/backspace"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:contentDescription="@string/backspace"
            android:src="@drawable/backspace"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

android android-layout android-imagebutton android-layout-weight

3
推荐指数
1
解决办法
206
查看次数

如何在android中的布局中对齐文本

我想在ListView行中显示一些具有以下对齐方式的文本:

Mike      10pt $1,233,500
Mike2    200pt $1,2,500
Holly     10pt $4500
Run Code Online (Sandbox Code Playgroud)

但是当我尝试时,我得到了这个:

   Mike        10pt   $1,233,500
    Mike2    200pt       $12,500
    Holly            10pt  $4500
Run Code Online (Sandbox Code Playgroud)

我的布局文件如下.我究竟做错了什么?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:background="@drawable/dl005_textfield_selected"
        android:paddingBottom="5dp" >

        <TextView
            android:id="@+id/Name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Mike"
            android:layout_weight="1"
            android:textColor="#888888" />

        <TextView
            android:id="@+id/score"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:gravity="right"
            android:text="10pt"
            android:textColor="#3498db" />

        <TextView
            android:id="@+id/myid"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:text="$1,233,500"
            android:textStyle="bold"
            android:textSize="18sp"
            android:layout_weight="1"
            android:gravity="right"
            android:textColor="#3498db" />

    </LinearLayout>

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

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

android android-layout android-layout-weight

2
推荐指数
1
解决办法
287
查看次数

将LinearLayout与DrawerLayout的底部对齐

我已经在应用程序的主要XML布局中实现了DrawerLayout,并且创建了一个页脚(基本上是水平[导航]栏),该页脚填充了屏幕的底部。问题是-它是不断地出现在屏幕的顶部...我已经尝试使用android:layout_gravity="bottom"android:layout_weight="1.0"android:baselineAligned="false"作为记录在这里:http://sandipchitale.blogspot.com/2010/05/linearlayout-gravity-and-layoutgravity。 html但是,我似乎仍然无法使页脚出现在布局的顶部。

任何建议表示赞赏:

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"
    android:layout_gravity="bottom"
    android:background="@color/black"
    android:baselineAligned="false" >



    ...



        <LinearLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:orientation="horizontal" >
    </LinearLayout>

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

完整的XML来源:

http://pastebin.com/67cHtwJ1

屏幕截图:

在此处输入图片说明

xml android android-layout layout-gravity android-layout-weight

2
推荐指数
1
解决办法
1701
查看次数

match_parent在自定义工具栏中不起作用

我制作了带搜索和设置图标的自定义工具栏.当我点击搜索图标时,edittext应该与match_parent一起使用,并且应该占用可用空间直到搜索图标.然而,它没有发生.它只是出现在左上方非常小的空间.请帮助我错了

在此输入图像描述

我想用xml分享我的整个代码

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DCDCDC"
android:padding="0dp"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="myresolver.faisal.home.com.myresolver.MainActivity">

<include
    android:id="@+id/toolbar"
    layout="@layout/my_toolbar"></include>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/toolbar"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="1dp"
        android:weightSum="4"

        >


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="3">

            <ImageView
                style="@style/icon"
                android:background="@drawable/fear_96" />

            <ImageView
                style="@style/icon"
                android:background="@drawable/fear_96"></ImageView>

            <ImageView
                style="@style/icon"
                android:background="@drawable/fear_96" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="3">

            <ImageView
                style="@style/icon"
                android:background="@drawable/fear_96" />

            <ImageView
                style="@style/icon"
                android:background="@drawable/fear_96" />

            <ImageView
                style="@style/icon"
                android:background="@drawable/fear_96" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="3">

            <ImageView
                style="@style/icon"
                android:background="@drawable/fear_96" /> …
Run Code Online (Sandbox Code Playgroud)

android toolbar android-linearlayout android-edittext android-layout-weight

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

Android - ConstraintLayout 中的 ConstraintVertical 权重不起作用?

我正在使用 from app:layout_constraintVertical_weight="5"inConstraintLayout但它不起作用。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/txtComingSoon"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#E1E2E4"
        android:text="New Courses Starting Soon.."
        android:textColor="#747577"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_weight="5"
        android:layout_marginTop="0dp" />

    <android.support.constraint.ConstraintLayout
        android:id="@+id/constImageButtons"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#FF9F00"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintVertical_weight="5"
        android:layout_marginBottom="0dp">

        <ImageButton
            android:id="@+id/imbRegister"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:background="@null"
            android:scaleType="fitXY"
            android:src="@drawable/ic_register"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageButton
            android:id="@+id/imbMedia"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:background="@null"
            android:scaleType="fitXY"
            android:src="@drawable/ic_media"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/imbRegister"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageButton
            android:id="@+id/imbWebinar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:background="@null"
            android:scaleType="fitXY"
            android:src="@drawable/ic_webinar"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/imbMedia"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageButton
            android:id="@+id/imbAboutUs" …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-layout-weight android-constraintlayout

2
推荐指数
1
解决办法
4255
查看次数

LinearLayout 2 views, if one is gone, it doesn't center the other, although I update the weightSum

this is my layout:

 <LinearLayout
        android:id="@+id/car_linear_layout"
        android:layout_toRightOf="@id/car_image"
        android:layout_width="wrap_content"
        android:orientation="vertical"
        android:layout_centerVertical="true"
        android:gravity="center_vertical"
        android:layout_height="50sp"
        android:weightSum="2">
        <TextView
            android:id="@+id/car_layout_name"
            android:textSize="17sp"
            android:maxLines="1"
            android:textColor="@color/black"
            android:ellipsize="end"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/car_layout_license"
            android:textSize="17sp"
            android:maxLines="1"
            android:ellipsize="end"
            android:textColor="@color/black"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

And this is my code:

    if (userVehicleID != null) {
        carLayout.setWeightSum(2);
        carLicense.setText("test");
    }else{
        carLayout.setWeightSum(1);
        carLicense.setVisibility(View.GONE);
    } 
Run Code Online (Sandbox Code Playgroud)

But even so, if I debug, and it enters the ELSE , it will show just the first textview, but it's not centered. why?

android android-linearlayout layout-gravity android-layout-weight

2
推荐指数
1
解决办法
853
查看次数