动态调整TextView宽度 本帖最后由 coxfilur_2008 于 2010-09-02 19:45:33 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 是 mEditText需要动态调整宽度。重发一下。 public void updateEditTextWidth(int screenWidth) { int editTextWidth = screenWidth; if (mNextBtn.getVisibility() == View.VISIBLE) { editTextWidth -= mNextBtn.getWidth(); editTextWidth -= mNextBtn.getPaddingLeft(); editTextWidth -= mNextBtn.getPaddingRight(); } if (mPrevBtn.getVisibility() == View.VISIBLE) { editTextWidth -= mPrevBtn.getWidth(); editTextWidth -= mPrevBtn.getPaddingLeft(); editTextWidth -= mPrevBtn.getPaddingRight(); } editTextWidth -= mSearchBtn.getWidth(); editTextWidth -= mSearchBtn.getPaddingLeft(); editTextWidth -= mSearchBtn.getPaddingRight(); editTextWidth -= mEditText.getPaddingLeft(); editTextWidth -= mEditText.getPaddingRight(); // temp implementation. 无理由地减去50,是因为算得宽度大。 editTextWidth -= 50; if (editTextWidth > 0 && mEditText.getWidth() != editTextWidth) { mEditText.setWidth(editTextWidth); mEditText.refreshDrawableState(); } } 如果更改xml配置(随便用什么layout,多少layout都行),能达到要求,请告知。谢谢! 感觉从布局上来看,可以不使用relativelayout。linearlayout更能满足楼主需要,只需要为每个控件设置android:layout_width="0dp"android:layout_weight="1"就好了。 其中1是由计算得来。贴上修改过的代码,一个思路(没有验证过~~~)<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fixed_linearLayout_search" android:layout_height="45dip" android:layout_width="fill_parent" android:paddingLeft="15dip" android:paddingRight="15dip" android:visibility="gone" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:orientation="horizontal" android:background="@color/solid_white"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/sub_btn_fixed_previous" android:textColor="@color/solid_black" android:layout_centerVertical="true" android:background="@drawable/transparent_background" android:drawableRight="@drawable/goto_previous" android:drawablePadding="5dip" android:paddingRight="10dip" android:textAppearance="?android:attr/textAppearanceSmall" > </Button> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal" > <EditText android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/fixed_search_content" android:layout_toRightOf="@+id/sub_btn_fixed_previous" android:layout_centerVertical="true"> </EditText> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/sub_btn_fixed_search" android:textColor="@color/solid_black" android:layout_centerVertical="true" android:background="@drawable/transparent_background" android:drawableRight="@drawable/menu_search" android:drawablePadding="5dip" android:textAppearance="?android:attr/textAppearanceSmall" > </Button> </LinearLayout> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/sub_btn_fixed_next" android:textColor="@color/solid_black" android:layout_centerVertical="true" android:background="@drawable/transparent_background" android:drawableRight="@drawable/goto_next" android:drawablePadding="5dip" android:textAppearance="?android:attr/textAppearanceSmall" > </Button> </LinearLayout> android:layout_width="0dp"android:layout_weight="1"够精辟的 android:layout_weight="1"通过layout_weight设置该控件“权重”,这样该控件就能自动的去适应布局了。用代码去实现,看你上面写的,觉得挺麻烦的。 基本可以。可是空隙比较大,就是EditText不能填充它应该填充的右边区域。不管是隐藏不隐藏(goto_next , goto_previous )的情况。不知道为何?看起来像这样(不会贴图片,写文字表示吧):// 4个View都显示出来的情况: Button [小空隙] TextView [比较大的空隙] Button [小空隙] Button// 两个button (goto_next,goto_previous,他们在显示时出于最右和最左)隐藏的情况。 TextView [比较大的空隙] Button 设置权重确实能起到作用。只是edittext的权重到底要设置多大,我不知道。 android+axis1.4开发客户端,jar包冲突,求解决! AVD 无法关闭 问个图片更新的问题 如何使图片产生液态效果 按键延迟处理问题 移植C++或者java代码到Android源码的步骤 关于Button 模拟器中音乐播放器显示乱码,寻求帮助 手机移动端开发问题sencha touch 和phonegap结合实现语音记录如何实现问题 android如何改变程序内所有View的字体大小 如何实现Home上屏幕背景图片的滑动 Android1.5 验证XML内容
重发一下。
public void updateEditTextWidth(int
screenWidth) {
int editTextWidth = screenWidth;
if (mNextBtn.getVisibility() == View.VISIBLE) {
editTextWidth -= mNextBtn.getWidth();
editTextWidth -= mNextBtn.getPaddingLeft();
editTextWidth -= mNextBtn.getPaddingRight();
}
if (mPrevBtn.getVisibility() == View.VISIBLE) {
editTextWidth -= mPrevBtn.getWidth();
editTextWidth -= mPrevBtn.getPaddingLeft();
editTextWidth -= mPrevBtn.getPaddingRight();
}
editTextWidth -= mSearchBtn.getWidth();
editTextWidth -= mSearchBtn.getPaddingLeft();
editTextWidth -= mSearchBtn.getPaddingRight(); editTextWidth -= mEditText.getPaddingLeft();
editTextWidth -= mEditText.getPaddingRight();
// temp implementation. 无理由地减去50,是因为算得宽度大。
editTextWidth -= 50; if (editTextWidth > 0 && mEditText.getWidth() != editTextWidth) {
mEditText.setWidth(editTextWidth);
mEditText.refreshDrawableState();
}
}
android:layout_width="0dp"
android:layout_weight="1"
就好了。 其中1是由计算得来。贴上修改过的代码,一个思路(没有验证过~~~)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fixed_linearLayout_search"
android:layout_height="45dip"
android:layout_width="fill_parent"
android:paddingLeft="15dip"
android:paddingRight="15dip"
android:visibility="gone"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="horizontal"
android:background="@color/solid_white">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sub_btn_fixed_previous"
android:textColor="@color/solid_black"
android:layout_centerVertical="true"
android:background="@drawable/transparent_background"
android:drawableRight="@drawable/goto_previous"
android:drawablePadding="5dip"
android:paddingRight="10dip"
android:textAppearance="?android:attr/textAppearanceSmall" >
</Button>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
>
<EditText android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/fixed_search_content"
android:layout_toRightOf="@+id/sub_btn_fixed_previous"
android:layout_centerVertical="true">
</EditText>
<Button android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/sub_btn_fixed_search"
android:textColor="@color/solid_black"
android:layout_centerVertical="true"
android:background="@drawable/transparent_background"
android:drawableRight="@drawable/menu_search"
android:drawablePadding="5dip"
android:textAppearance="?android:attr/textAppearanceSmall" >
</Button>
</LinearLayout> <Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sub_btn_fixed_next"
android:textColor="@color/solid_black"
android:layout_centerVertical="true"
android:background="@drawable/transparent_background"
android:drawableRight="@drawable/goto_next"
android:drawablePadding="5dip"
android:textAppearance="?android:attr/textAppearanceSmall" >
</Button>
</LinearLayout>
android:layout_weight="1"够精辟的
不管是隐藏不隐藏(goto_next , goto_previous )的情况。
不知道为何?看起来像这样(不会贴图片,写文字表示吧):// 4个View都显示出来的情况:
Button [小空隙] TextView [比较大的空隙] Button [小空隙] Button// 两个button (goto_next,goto_previous,他们在显示时出于最右和最左)隐藏的情况。
TextView [比较大的空隙] Button