使用的RelativeLayout控制一个ListView行高度 [英] Using RelativeLayout to control a ListView row height
问题描述
我有什么似乎我的谷歌搜索是一个罕见的问题。
我有一个定义的行应该是什么样子,我的ListView的XML文件。
由于我只希望看到一次一行我重写它工作在显示我只有一排巨大的适配器的getCount将。麻烦进来,尽管我的设置,以FILL_PARENT该行仍不能填满屏幕。
我已经试过无数的东西,我得到的最接近的是设置RelativeLayout的了minHeight但它是专用于屏幕尺寸。因此,无论我的元素不正确的RelativeLayout的设置或我需要一种方法根据设备自动调整了minHeight。
我想preFER为此在XML,但如果需要,可以在启动编程调整。
这是code父容器:
< RelativeLayout的
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT机器人:方向=垂直> < ListView控件
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT/>< / RelativeLayout的>
和该行的xml:
< RelativeLayout的
机器人:layout_width =FILL_PARENT
机器人:方向=垂直
机器人:layout_height =FILL_PARENT> < ImageView的
机器人:layout_width =60dp
机器人:layout_height =30dp
机器人:layout_alignParentLeft =真
机器人:layout_marginTop =15dp
机器人:layout_marginLeft =5DP/> <的TextView
机器人:layout_centerHorizontal =真
机器人:layout_width =185dp
机器人:layout_height =25dp
机器人:TEXTSIZE =20SP
机器人:layout_marginTop =15dp
机器人:比重=中心
安卓的inputType =无/> < ImageView的
机器人:layout_width =60dp
机器人:layout_height =30dp
机器人:layout_alignParentLeft =真
机器人:layout_marginTop =60dp
机器人:layout_marginLeft =5DP/>
<的TextView
机器人:layout_centerHorizontal =真
机器人:layout_width =185dp
机器人:layout_height =25dp
机器人:TEXTSIZE =20SP
机器人:layout_marginTop =60dp
机器人:比重=中心
安卓的inputType =无/> <的TextView
机器人:layout_alignParentLeft =真
机器人:layout_marginLeft =35dp
机器人:layout_width =95dp
机器人:layout_height =50dp
机器人:TEXTSIZE =18dp
机器人:layout_marginTop =110dp
机器人:比重=中心
安卓的inputType =无/> < org.gpgvm.widget.NumberPicker
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignRight =@ + ID / btn_ prevLift
机器人:layout_alignParentBottom =真
机器人:layout_alignParentLeft =真
机器人:layout_marginLeft =35dp
机器人:layout_marginBottom =120dp>
< /org.gpgvm.widget.NumberPicker> <的TextView
机器人:layout_alignParentRight =真
机器人:layout_marginRight =35dp
机器人:layout_width =95dp
机器人:layout_height =50dp
机器人:TEXTSIZE =18dp
机器人:layout_marginTop =110dp
机器人:比重=中心
安卓的inputType =无/> < org.gpgvm.widget.NumberPicker
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignLeft =@ + ID / btn_NextLift
机器人:layout_alignParentBottom =真
机器人:layout_alignParentRight =真
机器人:layout_marginRight =35dp
机器人:layout_marginBottom =120dp>
< /org.gpgvm.widget.NumberPicker> <按钮
机器人:layout_width =100dp
机器人:layout_height =40dp
机器人:layout_alignParentBottom =真
机器人:layout_alignParentLeft =真
机器人:layout_marginLeft =30dp
机器人:layout_marginBottom =30dp/> <按钮
机器人:layout_width =100dp
机器人:layout_height =40dp
机器人:layout_alignParentBottom =真
机器人:layout_alignParentRight =真
机器人:layout_marginRight =30dp
机器人:layout_marginBottom =30dp/>< / RelativeLayout的>
使用下面的code获得设备的屏幕的高度和动态设置了minHeight
DisplayMetrics指标=新DisplayMetrics();
。getWindowManager()getDefaultDisplay()getMetrics(指标)。 INT mHeight = metrics.heightPixels;
INT mWidth = metrics.widthPixels; RelativeLayout的yourLayout =(RelativeLayout的)findViewById(R.id.yourId);
RelativeLayout.LayoutParams newParams =新RelativeLayout.LayoutParams(mWidth,mHeight); yourLayout.setLayoutParams(newParams);
I have what seems by my Googling to be a rare question.
I have an XML file that defines what a row should look like in my ListView.
Since I only want to see one row at a time I override the getCount of the Adapter which works great at showing me only one row. The trouble comes in that despite my settings to fill_parent the row still doesn't fill the screen.
I have tried numerous things and the closest I get is to set the RelativeLayout minHeight but then it is specific to a screen size. So either my elements are not correctly setup in the RelativeLayout or I need a way to automatically adjust minHeight based upon device.
I would prefer to do this in XML but could adjust programatically at launch if needed.
This is the code for the parent container:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
and for the row xml:
<RelativeLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="fill_parent">
<ImageView
android:layout_width="60dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="15dp"
android:layout_marginLeft="5dp" />
<TextView
android:layout_centerHorizontal="true"
android:layout_width="185dp"
android:layout_height="25dp"
android:textSize="20sp"
android:layout_marginTop="15dp"
android:gravity="center"
android:inputType="none" />
<ImageView
android:layout_width="60dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="60dp"
android:layout_marginLeft="5dp" />
<TextView
android:layout_centerHorizontal="true"
android:layout_width="185dp"
android:layout_height="25dp"
android:textSize="20sp"
android:layout_marginTop="60dp"
android:gravity="center"
android:inputType="none" />
<TextView
android:layout_alignParentLeft="true"
android:layout_marginLeft="35dp"
android:layout_width="95dp"
android:layout_height="50dp"
android:textSize="18dp"
android:layout_marginTop="110dp"
android:gravity="center"
android:inputType="none" />
<org.gpgvm.widget.NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/btn_PrevLift"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="35dp"
android:layout_marginBottom="120dp">
</org.gpgvm.widget.NumberPicker>
<TextView
android:layout_alignParentRight="true"
android:layout_marginRight="35dp"
android:layout_width="95dp"
android:layout_height="50dp"
android:textSize="18dp"
android:layout_marginTop="110dp"
android:gravity="center"
android:inputType="none" />
<org.gpgvm.widget.NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn_NextLift"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="35dp"
android:layout_marginBottom="120dp" >
</org.gpgvm.widget.NumberPicker>
<Button
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="30dp"
android:layout_marginBottom="30dp" />
<Button
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="30dp"
android:layout_marginBottom="30dp" />
</RelativeLayout>
get the height of the screen of the device using the below code and set the minHeight dynamically
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int mHeight = metrics.heightPixels;
int mWidth = metrics.widthPixels;
RelativeLayout yourLayout= (RelativeLayout) findViewById(R.id.yourId);
RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(mWidth, mHeight);
yourLayout.setLayoutParams(newParams );
这篇关于使用的RelativeLayout控制一个ListView行高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!