Android 具有动态宽度的布局视图
我有一个布局,两个孩子一个接一个,像这样:Android 具有动态宽度的布局视图,android,android-layout,Android,Android Layout,我有一个布局,两个孩子一个接一个,像这样: |<view1><text1> | || 可能会更改其宽度。因此,我希望在两个视图都保持在屏幕上时,它会增加: |<really_wide_view1><text1> | || 但当右边没有空间时,它会停止: |<reeeeeeeeeeeally_wide_vi...><text1>| || 有没有简单的方法可
|<view1><text1> |
||
可能会更改其宽度。因此,我希望在两个视图都保持在屏幕上时,它会增加:
|<really_wide_view1><text1> |
||
但当右边没有空间时,它会停止:
|<reeeeeeeeeeeally_wide_vi...><text1>|
||
有没有简单的方法可以做到这一点
现在我尝试使用以下布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FF00aa00" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="loooooooooooooooooooooooooooooooooooong text" />
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#FFaa0000"
android:minWidth="30dp"
android:singleLine="true"
android:text="test" />
</LinearLayout>
但结果是一样的:尝试在RelativeLayout内提供两个视图,并将视图布局宽度和作为包裹内容,并将方向设置为水平。我想这会解决你的问题。试着在RelativeLayout内提供两个视图,并将视图布局为宽度和环绕内容,并将方向设置为水平。我想它会解决您的问题。我可以使用以下代码(您的修改版本)解决您的问题:
以下是相同的屏幕截图:
请告诉我上述代码是否解决了问题。我可以使用以下代码(您的修改版本)解决您的问题:
以下是相同的屏幕截图:
请告诉我上述代码是否解决了问题。这里是使用
TableLayout
的解决方案,但它看起来很脏
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:shrinkColumns="0" >
<TableRow>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
它适用于任意长度的两个文本视图。这里是使用
TableLayout
的解决方案,但它看起来很脏
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:shrinkColumns="0" >
<TableRow>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
它适用于任意长度的两个文本视图。在
线性布局中使用布局权重如何?
我已尝试设置布局权重=“1”
用于第二视图-但当第一个视图足够大时-第二个视图比屏幕右侧。我还尝试使用android:minWidth
用于第二视图-没有结果。请确保使用布局\u width=0dp
以便自动计算可用宽度。查看此问题的答案:使用layout\u weight
在LinearLayout
内部?我已尝试设置layout\u weight=“1”
用于第二视图-但当第一个视图足够大时,第二个视图比屏幕右侧。我还尝试使用android:minWidth
用于第二视图-没有结果。请确保使用layout\u width=0dp
以便自动计算可用的宽度。查看此问题的答案:这不起作用。如果视图之间没有关系,那么它会覆盖彼此。如果我使用toRightOf
进行第二次查看,它会从屏幕上消失,这不起作用。如果视图之间没有关系,那么它会覆盖彼此。如果我使用toRightOf
进行第二次查看,它将从屏幕上消失。这解决了长文本的问题,但现在它对短文本的作用是错误的。我已经找到了TableView的解决方案,我将在五分钟后发布。这解决了长文本的问题,但现在它对短文本起错了作用。我已经找到了TableView的解决方案,我将在五分钟后发布。我本来打算发布一个答案,但你已经找到了TableLayout
解决方案。我在这里回答了一个类似的问题。基本上不需要添加线性布局
,您只需将android:shrinkColumns=“0”android:stretchColumns=“1”
添加到您的表格布局
线性布局
就像容器一样,里面会有一些复杂的视图。我本来想发布一个答案,但你已经找到了TableLayout
解决方案。我在这里回答了一个类似的问题。基本上不需要添加LinearLayout
,您只需将android:shrinkColumns=“0”android:stretchColumns=“1”
添加到您的表格布局LinearLayout
就像容器一样,里面会有一些复杂的视图。