- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我需要在一行中显示两个按钮,但如果第一个按钮占满宽度(如果没有空间),则第二个按钮必须向下移动(不重叠/展平)。
我尝试了以下代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Buttonttttttttttttttttttttsdfssdfsdfsdf"
android:id="@+id/button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"/>
</LinearLayout>
</LinearLayout>
但有以下问题:
请问如何解决这个问题?也许使用 RelativeLayout?
最佳答案
我和你有类似的疑问。这是我最终使用的解决方案。
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
public class WritingLayout extends ViewGroup
{
private int[] rowHeights;
public WritingLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int maxInternalWidth = MeasureSpec.getSize(widthMeasureSpec) - getHorizontalPadding();
int maxInternalHeight = MeasureSpec.getSize(heightMeasureSpec) - getVerticalPadding();
List<RowMeasurement> rows = new ArrayList<RowMeasurement>();
RowMeasurement currentRow = null;
for (View child : getLayoutChildren())
{
LayoutParams childLayoutParams = (LayoutParams)child.getLayoutParams();
int childWidthSpec = createChildMeasureSpec(childLayoutParams.width, maxInternalWidth, widthMode);
int childHeightSpec = createChildMeasureSpec(childLayoutParams.height, maxInternalHeight, heightMode);
child.measure(childWidthSpec, childHeightSpec);
int childWidth = child.getMeasuredWidth();
int childHeight = child.getMeasuredHeight();
if ((currentRow == null) || currentRow.isWouldExceedMax(childWidth))
{
currentRow = new RowMeasurement(maxInternalWidth, widthMode);
rows.add(currentRow);
}
currentRow.addChildDimensions(childWidth, childHeight);
}
int longestRowWidth = 0;
int totalRowHeight = 0;
int rowCount = rows.size();
this.rowHeights = new int[rowCount];
for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
{
RowMeasurement row = rows.get(rowIndex);
int rowHeight = row.getHeight();
this.rowHeights[rowIndex] = rowHeight;
totalRowHeight = totalRowHeight + rowHeight;
longestRowWidth = Math.max(longestRowWidth, row.getWidth());
}
setMeasuredDimension((widthMode == MeasureSpec.EXACTLY) ? MeasureSpec.getSize(widthMeasureSpec) : (longestRowWidth + getHorizontalPadding()), (heightMode == MeasureSpec.EXACTLY) ? MeasureSpec.getSize(heightMeasureSpec) : (totalRowHeight + getVerticalPadding()));
}
private int createChildMeasureSpec(int childLayoutParam, int max, int parentMode)
{
int spec;
if (childLayoutParam == ViewGroup.LayoutParams.FILL_PARENT)
spec = MeasureSpec.makeMeasureSpec(max, MeasureSpec.EXACTLY);
else if (childLayoutParam == ViewGroup.LayoutParams.WRAP_CONTENT)
spec = MeasureSpec.makeMeasureSpec(max, ((parentMode == MeasureSpec.UNSPECIFIED) ? MeasureSpec.UNSPECIFIED : MeasureSpec.AT_MOST));
else
spec = MeasureSpec.makeMeasureSpec(childLayoutParam, MeasureSpec.EXACTLY);
return spec;
}
@Override
protected LayoutParams generateDefaultLayoutParams()
{
return new LayoutParams();
}
@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams layoutParams)
{
return (layoutParams instanceof LayoutParams);
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs)
{
return new LayoutParams(getContext(), attrs);
}
@Override
protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p)
{
return new LayoutParams(p);
}
@Override
protected void onLayout(boolean changed, int leftPosition, int topPosition, int rightPosition, int bottomPosition)
{
int widthOffset = getMeasuredWidth() - getPaddingRight();
int x = getPaddingLeft();
int y = getPaddingTop();
int rowIndex = 0;
for (View child : getLayoutChildren())
{
int childWidth = child.getMeasuredWidth();
int childHeight = child.getMeasuredHeight();
LayoutParams layoutParams = (LayoutParams)child.getLayoutParams();
if ((x + childWidth) > widthOffset)
{
x = getPaddingLeft()+rightPosition-childWidth;
y = y + this.rowHeights[rowIndex];
rowIndex = rowIndex + 1;
}
int _y;
if (layoutParams.centerVertical)
_y = y + ((this.rowHeights[rowIndex] - childHeight) / 2);
else
_y = y;
child.layout(x, _y, x + childWidth, _y + childHeight);
x = x + childWidth;
}
}
private Collection<View> getLayoutChildren()
{
int count = getChildCount();
Collection<View> children = new ArrayList<View>(count);
for (int index = 0; index < count; index++)
{
View child = getChildAt(index);
if (child.getVisibility() != View.GONE)
children.add(child);
}
return children;
}
private int getVerticalPadding()
{
return getPaddingTop() + getPaddingBottom();
}
private int getHorizontalPadding()
{
return getPaddingLeft() + getPaddingRight();
}
public static class LayoutParams extends MarginLayoutParams
{
boolean centerVertical;
public LayoutParams()
{
this(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}
public LayoutParams(int width, int height)
{
super(width, height);
setCenterVertical(false);
}
public LayoutParams(Context context, AttributeSet attrs)
{
super(context, attrs);
TypedArray attributes = context.obtainStyledAttributes(attrs, new int[] { android.R.attr.layout_centerVertical });
setCenterVertical(attributes.getBoolean(0, false));
attributes.recycle();
}
public LayoutParams(ViewGroup.LayoutParams source)
{
super(source);
}
public LayoutParams(MarginLayoutParams source)
{
super(source);
}
public LayoutParams(LayoutParams source)
{
this(source.width, source.height);
}
public void setCenterVertical(boolean centerVertical)
{
this.centerVertical = centerVertical;
}
}
private static class RowMeasurement
{
private int maxWidth;
private int widthMode;
private int width;
private int height;
RowMeasurement(int maxWidth, int widthMode)
{
this.maxWidth = maxWidth;
this.widthMode = widthMode;
}
int getWidth()
{
return this.width;
}
int getHeight()
{
return this.height;
}
boolean isWouldExceedMax(int childWidth)
{
return ((this.widthMode != MeasureSpec.UNSPECIFIED) && (getNewWidth(childWidth) > this.maxWidth));
}
void addChildDimensions(int childWidth, int childHeight)
{
this.width = getNewWidth(childWidth);
this.height = Math.max(this.height, childHeight);
}
private int getNewWidth(int childWidth)
{
return ((this.width == 0) ? childWidth : (this.width + childWidth));
}
}
}
使用此 WritingLayout 而不是您现在使用的布局。
关于Android - 如果没有空间,请向下移动按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24949682/
我正在使用面部跟踪进行 HCI(人机交互)。我正在尝试使用面部控制 PC。 我有 3x3 二维网格按钮。1 2 34 5 67 8 9 假设,当前焦点在按钮 5 上。如果我按向上箭头,则焦点将在 2
我正在为蛇和梯子制作一 block 板,到目前为止,我已经按降序打印了板。但是,我需要以正确的方式打印电路板。 编辑“螺旋下降”意味着 100...91 81...90 80...71 ...
我有一个可以响应式调整大小的菜单,因此每次调整大小时 div (#menuWFhover) 的内容都会重新排列,因此 div 根据窗口大小具有不同的高度。 当我使用 jQuery slideDown/
我们从服务器获取数据并附加 ListView 。我们的问题是,当向上/向下滚动时,它首先显示白屏,然后显示数据。向下/向上滚动时,之前出现的白屏应删除 for (var i=0; i");
我想要一个汉堡图标动态改变颜色的代码,以适应网站的黑色部分/部分和白色。它最初是 3 段白色,js 代码几乎是好的和正确的,但是当它在白色部分时它一直闪烁,并在红色和白色之间闪烁。 js var to
我对点击事件的页脚位置有疑问。我正在使用 bootstrap css,我有一个可折叠元素(bootstrap 中的 Accordion ),当它折叠时它有一个名为 .accordion-toggle.
在此代码段中,使用关键帧和动画并显示无/ block ,div 动画以在悬停时向下滑动。 h1 { padding: 20px; } div { width: 100%; background
我需要对一个 float 进行四舍五入。例如 4.00011 。内置函数 round() 总是在数字 > .5 时向上舍入,在 = 0 val *= 10 ** precision r
我正在尝试就地缩小文件。 我正在用另一个文件的内容替换一个文件的内容,完成后我想确保源文件是否小于目标文件,目标文件是否正确收缩。 (为什么:因为dest文件是一个备份,写入media的开销很大,所以
似乎每当我用一个负整数除以一个正整数时,我都需要它向下舍入 (向 -inf),而不是向 0。但 C# 和 C++ 都向 0 舍入。 所以我想我需要一个 DivideDownward() 方法。我可以用
考虑这个简单的代码: document.addEventListener( 'keypress', function() { console.log( 'press' ); } ); document
有什么方法可以检查 Azure 资源(例如 Azure IoT 中心或事件中心)是否可用。我所说的可用是指它是否已关闭/工作/正常/启动?我是 Azure 的新手,如果有人能提供一些启发,那就太好了。
有什么方法可以检查 Azure 资源(例如 Azure IoT 中心或事件中心)是否可用。我所说的可用是指它是否已关闭/工作/正常/启动?我是 Azure 的新手,如果有人能提供一些启发,那就太好了。
我见过几个recyclerview的无限滚动示例,但它们都是向下滚动的。 我想要的是一次又一次地加载我的列表,但无论用户是从上面还是从下面滚动。 正常列表 向上滚动 向下滚动 这样可以通过无限滚动一遍
我正在使用带有 View 的 drupal 7,并且我正在使用我发现的向上/向下文本幻灯片的 jquery 脚本。它有效,但是当我尝试将它与带有 ajax 的 View 公开过滤器一起使用时,它似乎不
*场景 我希望制作一个类似于本网站上的 jquery 效果,它位于主 Flash 添加的右侧: http://www.commbank.com.au/ *问题 我已经开始了,但是用很多方法遇到了一些障
互联网。如果这与其他人没有什么关系,请原谅我,但我会将其留在这里,以防这是一个有效的问题。 我正在尝试创建一个文本区域字段,其中用户每次按下键(a-z),都会触发背景颜色更改(在数组中列出)。我一直在
我正在创建一个网络前端来控制一个小型机器人。 Ajax 调用将在 keydown 上进行,以启动机器人,并在 keyup 上进行停止。 我的问题是,当按下某个键时,keyup、keydown 和 ke
我在内容容器中有两个 div,一个向左浮动,另一个向右浮动。我正在使用屏幕的整个宽度。左 div 宽度为 1290px,右 div 宽度为 625px。有时,在加载页面时,滚动条会更改可用屏幕宽度的宽
请看这个UI sketch图片,我在某个站点的侧边栏(黑框)中有这个 div,当我向下滚动或向上滚动时,我不希望它隐藏...我希望它在我向下滚动和移动时自行向下移动当我向上滚动时向上滚动,这样它就永远
我是一名优秀的程序员,十分优秀!