标签: android-layout-weight

Android中的线性布局和重量

我总是在Android文档中读到这个有趣的重量值.现在我想第一次尝试它,但它根本不工作.

据我所知,这个布局的文件:

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

     <Button
        android:text="Register"
        android:id="@+id/register"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dip"
        weight="1" />

     <Button
        android:text="Not this time"
        android:id="@+id/cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dip"
        weight="1" />

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

应创建两个水平对齐的按钮,并平等分享空间.问题是两个按钮不会增长以填充空间.

我想按钮增长并填满整行.如果两个按钮都设置为仅匹配父按钮,则显示第一个按钮并填充整行.

android android-linearlayout android-layout-weight

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

如何从代码中动态设置layout_weight属性?

如何layout_weight从java代码中动态设置android中按钮的属性值?

android android-layout android-layout-weight

224
推荐指数
7
解决办法
21万
查看次数

以编程方式设置TextView的布局权重

我正在尝试动态创建TableRow对象并将其添加到TableLayout.该TableRow对象有2个项目,一个TextViewCheckBox.该TextView项目需要有自己的布局权重设置为1,以推动CheckBox项目的最右边.

我找不到有关如何以编程方式设置项目的布局权重的文档TextView.

layout android textview android-layout-weight

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

什么是android:android中的weightSum,它是如何工作的?

我想知道:什么是android:weightSum和布局权重,它们是如何工作的?

android android-layout android-layout-weight

154
推荐指数
7
解决办法
15万
查看次数

android:layout_height屏幕大小的50%

大家好我刚刚在LinearLayout中实现了ListView,但我需要定义LinearLayout的高度(它必须是屏幕高度的50%).

<LinearLayout
    android:id="@+id/widget34"
    android:layout_width="300px"
    android:layout_height="235px"
    android:orientation="vertical"
    android:layout_below="@+id/tv_scanning_for"
    android:layout_centerHorizontal="true">

    <ListView
        android:id="@+id/lv_events"
        android:textSize="18sp"         
        android:cacheColorHint="#00000000"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_scanning_for"
        android:layout_centerHorizontal="true">
    </ListView>

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

这有可能吗?

我为一个按钮和一个EditText做了类似的事情,但似乎不适用于Layouts.

这是我的代码:

    //capture the size of the devices screen
    Display display = getWindowManager().getDefaultDisplay();
    double width = display.getWidth();

    //my EditText will be smaller than full screen (80%)        
    double doubleSize = (width/5)*4;
    int editTextSize = (int) doubleSize;

    //define the EditText 
    userName = (EditText) this.findViewById(R.id.userName);
    password = (EditText) this.findViewById(R.id.password);

    //set the size
    userName.setWidth(editTextSize);
    password.setWidth(editTextSize);
Run Code Online (Sandbox Code Playgroud)

谢谢!:)

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

45
推荐指数
3
解决办法
13万
查看次数

Android如何一个接一个地在一个活动中显示2个列表视图

我已经使用此代码在另一个上面显示了2个列表视图.

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

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="#f00" >
</ListView>

<ListView
    android:id="@+id/listView2"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="#0f0" >
</ListView>
Run Code Online (Sandbox Code Playgroud)

问题是,这导致每个列表视图占据屏幕的一半.我正在为这两个列表添加一个标题.

LevelAdapter adapter = new LevelAdapter(getActivity(),
            R.layout.list_item, weather_data);

    View header = inflater.inflate(R.layout.header2, null);
    View header2 = inflater.inflate(R.layout.header, null);
   lv1.addHeaderView(header);
   lv2.addHeaderView(header2);
    lv1.setAdapter(adapter);
    lv2.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)

我想在第一个列表结束后出现第二个列表的标题.我该怎么做?如何使列表视图出现,以便第一个在第一个结束时开始?谢谢

android listview android-layout-weight

41
推荐指数
4
解决办法
7万
查看次数

如何以编程方式将android:layout_columnWeight ="1"设置为android支持v7 Gridlayout中的元素

我正在尝试以编程方式使用2列构建GirdLayout,并且我希望这些列的宽度设置为屏幕宽度的一半.

我想你可以从API 21或支持v7 GirdLayout视图执行此操作.我看到使用android:layout_columnWeight ="1"的示例来执行此操作.但我找不到如何以编程方式执行此操作.

任何人都可以帮我解决这个问题吗?

package com.tunify.v3.component;

import java.util.ArrayList;

import android.support.v7.widget.GridLayout;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.ViewGroup;

import com.tunify.asme.data.ChangeableSelectionParameterValue;

public class ChangeableSelectionParameters extends GridLayout{

//private ArrayList<ChangeableSelectionParameterView> selectionParameterViews;
private ArrayList<ChangeableSelectionParameterValue> selectionParameterValues;

private static final int ITEM_PADDING_LEFT_DP = 0;
private static final int ITEM_PADDING_RIGHT_DP = 0;
private static final int ITEM_PADDING_TOP_DP = 0;
private static final int ITEM_PADDING_BOTTOM_DP = 0;

//private static final int MUSICCOLLECTION_DISPLAY_TEXT_SIZE_SP = 15;


public ChangeableSelectionParameters(android.content.Context context, ArrayList<ChangeableSelectionParameterValue> selectionParameterValues) {
    super(context);

    this.selectionParameterValues = selectionParameterValues;

    initializeLayoutBasics(context);
    initializeComponents();
}

private …
Run Code Online (Sandbox Code Playgroud)

android android-gridlayout android-layout-weight

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

Android线性布局重量以编程方式

我想以编程方式将三个线性布局添加到每个相同宽度的活动中.问题是我无法以编程方式设置这些布局的权重.我可以在xml中执行此操作,但我想在程序中执行此操作.这就是我想要的: 在此输入图像描述

android android-linearlayout android-layout-weight

26
推荐指数
2
解决办法
3万
查看次数

Android:试图了解android:layout_weight

我试图将页面分成三部分.我想用百分比值来做,但Android不支持.相反,我必须使用android:layout_weight.但我很难理解并正确行事.特别是如何计算实际大小.有没有办法获得百分比值(0..100%)android:layout_weight

我经历了几个案例(见附页截图)来描述问题.彩色场都<LinearLayout>android:layout_height="fill_parent",因为我想那些之间进行划分整个屏幕.

情况1

替代文字

好的,简单.每人<LinearLayout>得到33%.

案例2

替代文字

UPS?!第一个(黄色)<LinearLayout>完全消失了?为什么?

案例3

替代文字

又困惑了.黄色<LinearLayout>又回来了.然而,<LinearLayout>重量较重的两个首先变得更小?到底是怎么回事?

案例4

替代文字

我完全不知道这一切背后的数学是什么.

layout android android-layout-weight

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

替换linearLayout权重机制

背景:

  • 由于性能原因,Google建议避免使用嵌套加权linearLayouts.
  • 使用嵌套加权linearLayout很难读,写和维护.
  • 对于放置可用大小百分比的视图,仍然没有其他好的选择.只有解决方案才是权重并使用OpenGL.甚至没有类似WPF/Silverlight上显示的"viewBox"来自动缩放内容.

这就是为什么我决定创建我自己的布局,你要告诉每个孩子他们的重量(和周围的重量)与它的大小相比应该是什么.

看来我已经成功了,但出于某种原因,我认为有一些我无法追查的错误.

其中一个错误就是textView,即使我为它提供了大量空间,它也会将文本放在顶部而不是放在中心.另一方面,imageViews工作得很好.另一个错误是,如果我在自定义布局中使用布局(例如frameLayout),则其中的视图将不会显示(但布局本身将会显示).

请帮我弄清楚它为什么会发生.

如何使用:而不是线性布局的下一个用法(我故意使用长XML,以显示我的解决方案如何缩短事物):

<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="match_parent" android:orientation="vertical">

  <View android:layout_width="wrap_content" android:layout_height="0px"
    android:layout_weight="1" />

  <LinearLayout android:layout_width="match_parent"
    android:layout_height="0px" android:layout_weight="1"
    android:orientation="horizontal">

    <View android:layout_width="0px" android:layout_height="wrap_content"
      android:layout_weight="1" />

    <TextView android:layout_width="0px" android:layout_weight="1"
      android:layout_height="match_parent" android:text="@string/hello_world"
      android:background="#ffff0000" android:gravity="center"
      android:textSize="20dp" android:textColor="#ff000000" />

    <View android:layout_width="0px" android:layout_height="wrap_content"
      android:layout_weight="1" />

  </LinearLayout>
  <View android:layout_width="wrap_content" android:layout_height="0px"
    android:layout_weight="1" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我所做的只是(x是将视图本身放在权重列表中的位置):

<com.example.weightedlayouttest.WeightedLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res/com.example.weightedlayouttest"
  xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  android:layout_height="match_parent" tools:context=".MainActivity">

  <TextView android:layout_width="0px" android:layout_height="0px"
    app:horizontalWeights="1,1x,1" app:verticalWeights="1,1x,1"
    android:text="@string/hello_world" android:background="#ffff0000"
    android:gravity="center" android:textSize="20dp" android:textColor="#ff000000" />

</com.example.weightedlayouttest.WeightedLayout>
Run Code Online (Sandbox Code Playgroud)

我的特殊布局代码是:

public class WeightedLayout extends ViewGroup
  {
  @Override …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-linearlayout android-layout-weight android-percent-library

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