Android 将布局重量与最小宽度相结合
我有一个布局,我想分为2个视图使用布局_重量(1:2)。但是,我希望左视图的宽度至少为400dp 例如,如果左视图通过使用权重获得420dp宽度,则将其保留,但如果左视图的宽度小于400dp,则将其设置为400dp,并将其余所有内容都提供给另一个视图 这是我尝试过的布局,但不适合我Android 将布局重量与最小宽度相结合,android,android-layout,Android,Android Layout,我有一个布局,我想分为2个视图使用布局_重量(1:2)。但是,我希望左视图的宽度至少为400dp 例如,如果左视图通过使用权重获得420dp宽度,则将其保留,但如果左视图的宽度小于400dp,则将其设置为400dp,并将其余所有内容都提供给另一个视图 这是我尝试过的布局,但不适合我 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_w
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:minWidth="400dp"
android:background="@android:color/holo_blue_bright"/>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="@android:color/holo_green_light"/>
</LinearLayout>
请帮忙,
谢谢 试试这个:
我想这就是您需要的:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<View
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:minWidth="400dp"
android:background="@android:color/holo_blue_bright"/>
<View
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light"/>
</LinearLayout>
基本上,让第一个布局占用所需的空间,但不少于400dp。第二个将占用所有剩余的资源。与所有涉及重量的情况一样,请确保两个孩子所需的空间(宽度)小于家长所能提供的空间,否则您将无法看到屏幕上的内容
注意:我在手机布局上尝试过,但400dp在屏幕上是纵向的,所以第一个布局似乎占用了所有空间,所以请确保在您希望布局跨度超过400dp的设备上进行尝试:-)。但在此之前,我建议重新考虑你的用户界面。您试图做的事情似乎有点多余。请将您的android:layout\u width=“match\u parent”替换为LinearLayout的android:layout\u width=“wrap\u content”。很抱歉没有在问题中写下这一点,但左视图是一个动态布局,应该从父视图获取大小。无论如何谢谢你!这不会改变任何事情。这样做的目的是不给它施加压力。如果您实际上通过
LayoutParams
设置了宽度,而没有将其设置为wrap\u content
,这并不重要。也许您需要将视图
标记替换为线性布局
,并将视图
放在其中。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<View
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:minWidth="400dp"
android:background="@android:color/holo_blue_bright"/>
<View
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light"/>
</LinearLayout>