Android关于布局的问题

superstar0013 2015-11-02 10:41:20
大体情况是这样的:有两个View(称为View1heView2),View1与父容器的顶端和左端对齐,且大小固定;View2位置在View1的右端,宽和高设置成了:match-parent,只是占满了父容器剩余的空间。但要是将View2的位置改为与父容器的右端对齐,其它不变,则会覆盖整个父容器,请问一下为什么?还有我的这种方法可取吗,会不会出现什么bug?
...全文
175 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_27397779 2015-11-04
  • 打赏
  • 举报
回复
这应该是一个相对布局吧,不要用拖动的,直接写代码 View2设置toRightof View1 或者margin View1的宽度 再设置成match应该就没有问题,如果不加前面的属性,直接match肯定是会覆盖的
Arnold9009 2015-11-03
  • 打赏
  • 举报
回复
简单说,lz这样的用法,只有功能实现了,测试ok,就没什么问题 但是这是依赖framework层的measure和layout的实现逻辑,万一谷歌在新版本上稍微修改一下,可能会出问题 其次,个人认为更重要的是,这样的写法很难维护,因为直接根据代码去推理具体的效果比较困难
  • 打赏
  • 举报
回复
android里面好像没有LayoutManger这个概念。就是View和ViewGroup这两个用户接口的概念,嵌套起来指定width,,height,weight这个些属性
  • 打赏
  • 举报
回复
你是在哪学了这么长时间的android。。。。
你知道什么是View什么是ViewGroup么?。。所有的ViewGroup的子类都可以用来布局,看这个:

官方说的很清除了,尽量不要去潜逃你的ViewGroup(也就是你的布局器)

你搞那么 多LinearLayout干嘛。。。
中才德创 2015-11-02
  • 打赏
  • 举报
回复
match-parent是不行的。它是相对父来说的,并不理会你的左边是什么。这个得用weightt。 给你一个参考:
<?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="match_parent"
		android:layout_height="0dp"
		android:orientation="horizontal"
		android:layout_weight="1"
		android:weightSum="2" >
		<Button
			android:id="@+id/btnBus00"
			android:text="btnBus00"
			android:layout_width="0dp"
			android:layout_height="match_parent"
			android:layout_weight="1" />
		<Button
			android:id="@+id/btnBus01"
			android:text="btnBus01"
			android:layout_width="0dp"
			android:layout_height="match_parent"
			android:layout_weight="1" />
	</LinearLayout>
	<LinearLayout
		android:layout_width="match_parent"
		android:layout_height="0dp"
		android:orientation="horizontal"
		android:layout_weight="1"
		android:weightSum="2" >
		<Button
			android:id="@+id/btnBus10"
			android:text="btnBus10"
			android:layout_width="0dp"
			android:layout_height="match_parent"
			android:layout_weight="1" />
		<Button
			android:id="@+id/btnBus11"
			android:text="btnBus11"
			android:layout_width="0dp"
			android:layout_height="match_parent"
			android:layout_weight="1" />
	</LinearLayout>
</LinearLayout>
superstar0013 2015-11-02
  • 打赏
  • 举报
回复
引用 4 楼 qq_28224387 的回复:
2,你最好还是考虑使用列表或是网格好了。ListLayout或GridLayout
还有哥们,我学Android这么长时间了就没听说过你说的这两个布局(难道是自定义控件)?我只听说过TableLayout和GridView!
superstar0013 2015-11-02
  • 打赏
  • 举报
回复
引用 4 楼 qq_28224387 的回复:
2,你最好还是考虑使用列表或是网格好了。ListLayout或GridLayout
为什么不可取,就没人用过这个方法吗?还有有什么方法能达到相同的效果?
  • 打赏
  • 举报
回复
2,你最好还是考虑使用列表或是网格好了。ListLayout或GridLayout
  • 打赏
  • 举报
回复
首先,你这种方法是不可取的! 1, 你可以像1楼说的设置view1,view2的权重(width-weight),view1设小点,view2设大了。
superstar0013 2015-11-02
  • 打赏
  • 举报
回复
引用 1 楼 HawkOfWinter 的回复:
match-parent是不行的。它是相对父来说的,并不理会你的左边是什么。这个得用weightt。 给你一个参考:
<?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="match_parent"
		android:layout_height="0dp"
		android:orientation="horizontal"
		android:layout_weight="1"
		android:weightSum="2" >
		<Button
			android:id="@+id/btnBus00"
			android:text="btnBus00"
			android:layout_width="0dp"
			android:layout_height="match_parent"
			android:layout_weight="1" />
		<Button
			android:id="@+id/btnBus01"
			android:text="btnBus01"
			android:layout_width="0dp"
			android:layout_height="match_parent"
			android:layout_weight="1" />
	</LinearLayout>
	<LinearLayout
		android:layout_width="match_parent"
		android:layout_height="0dp"
		android:orientation="horizontal"
		android:layout_weight="1"
		android:weightSum="2" >
		<Button
			android:id="@+id/btnBus10"
			android:text="btnBus10"
			android:layout_width="0dp"
			android:layout_height="match_parent"
			android:layout_weight="1" />
		<Button
			android:id="@+id/btnBus11"
			android:text="btnBus11"
			android:layout_width="0dp"
			android:layout_height="match_parent"
			android:layout_weight="1" />
	</LinearLayout>
</LinearLayout>
可是我的代码就实现了这个功能,并且我在网上也找到了相同的例子,网址如下:http://www.nikest.com/web/jswd/2015/0309/136010.html,但还是不清楚为什么,所以想了解一下!

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧