Android 如何使用权重创建自定义布局?
为了占据屏幕的所有空间,我做了如下布局:Android 如何使用权重创建自定义布局?,android,android-layout,android-layout-weight,Android,Android Layout,Android Layout Weight,为了占据屏幕的所有空间,我做了如下布局: <?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:orientation="ve
<?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:orientation="vertical"
android:weightSum="2" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_detail_placename"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:text="zeudhzeid" />
<TextView
android:id="@+id/tv_detail_description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:text="Descr d zedezdezdzedez dez dezd" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="vertical" >
<!-- Map -->
<fragment
android:id="@+id/map_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
class="com.google.android.gms.maps.SupportMapFragment"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Do" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Do" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Do" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Do" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
在Eclipse中,布局似乎很好,这是Graphica layout给出的视图:
但在现实中,在我的手机(三星Galaxy S3)中,我发现:
至于信息,我使用了
ActionBarSherlock
作为主题。你用xml声明了新的滚动条,所有东西都在其中。映射大于priview的默认测量尺寸。
同样地
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_detail_placename"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:text="zeudhzeid" />
<TextView
android:id="@+id/tv_detail_description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:text="Descr d zedezdezdzedez dez dezd" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="vertical" >
<!-- Map -->
<fragment
android:id="@+id/map_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
class="com.google.android.gms.maps.SupportMapFragment"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Do" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Do" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Do" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Do" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
试着根据需要调整
碎片的重量
<fragment
android:id="@+id/map_detail"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="2"
class="com.google.android.gms.maps.SupportMapFragment"/>
使用重量时,请记住将高度或宽度设置为0dp。也许这就是问题所在。
例如,你应该替换这个
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Do" />
用这个
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Do" />
您还应该知道嵌套权重对性能没有好处。你应该重新考虑你的布局。
<fragment
android:id="@+id/map_detail"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="2"
class="com.google.android.gms.maps.SupportMapFragment" />
我希望这能解决问题。尝试设置碎片高度0dip
。谢谢,但此滚动视图与我的重量设置和结合谷歌地图的滚动视图不能正常工作==BAD您应该为碎片设置固定大小它可以工作是的!但是现在我的地图太短了。。你有没有一个解决方案来保持这个重量系统,但只是减少按钮的价值?