我总是在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)
应创建两个水平对齐的按钮,并平等分享空间.问题是两个按钮不会增长以填充空间.
我想按钮增长并填满整行.如果两个按钮都设置为仅匹配父按钮,则显示第一个按钮并填充整行.
如何layout_weight从java代码中动态设置android中按钮的属性值?
我正在尝试动态创建TableRow对象并将其添加到TableLayout.该TableRow对象有2个项目,一个TextView和CheckBox.该TextView项目需要有自己的布局权重设置为1,以推动CheckBox项目的最右边.
我找不到有关如何以编程方式设置项目的布局权重的文档TextView.
我想知道:什么是android:weightSum和布局权重,它们是如何工作的?
大家好我刚刚在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
我已经使用此代码在另一个上面显示了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)
我想在第一个列表结束后出现第二个列表的标题.我该怎么做?如何使列表视图出现,以便第一个在第一个结束时开始?谢谢
我正在尝试以编程方式使用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) 我想以编程方式将三个线性布局添加到每个相同宽度的活动中.问题是我无法以编程方式设置这些布局的权重.我可以在xml中执行此操作,但我想在程序中执行此操作.这就是我想要的:

我试图将页面分成三部分.我想用百分比值来做,但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

我完全不知道这一切背后的数学是什么.
背景:
这就是为什么我决定创建我自己的布局,你要告诉每个孩子他们的重量(和周围的重量)与它的大小相比应该是什么.
看来我已经成功了,但出于某种原因,我认为有一些我无法追查的错误.
其中一个错误就是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