2012-10-05 117 views
-2

我想在android应用程序中设置布局,以便编辑文本占据大部分屏幕并且按钮位于底部。我该如何动态地做到这一点?在android中设置布局

screenshot http://i46.tinypic.com/71ldf5.png

虽然我想与所有剩余空间均衡出文本字段。

在此先感谢。

这个问题已经得到解答,我得到了解决方案。那么为什么在世界上它被标记为“不具有建设性”呢?

这设置了一个示例如何在android xml定义中给出权重。没有冒犯,但我觉得那些投票拒绝这个职位的人是吸血鬼,他们正在四处漫游,通过得到任何种类的借口来吸血,即使它是微不足道的。

如果社区里有人支持公开交流,请来这里看看巨魔如何摧毁好的知识收集。

最后我希望在前三段中我已经清楚了。

回答

0

很容易

<?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" > 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:ems="10" 
     android:gravity="top|left" > 

     <requestFocus /> 
    </EditText> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

</LinearLayout> 
+0

首先,我要动态地做到这一点。其次,当我试图动态设置它时,它没有奏效。 http://snipt.org/vWaj4 –

+0

如何设置重量以动态填充空间?毫无疑问,它可以在XML中完成,但我该如何动态地执行它? –

0

编辑:

RelativeLayout layout = new RelativeLayout(this); 

layout.setScrollContainer(true); // Remove this if you would not like the soft keyboard to resize the view 
LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 

EditText text = new EditText(this); 
text.setGravity(Gravity.TOP | Gravity.LEFT); 

Button button = new Button(this); 
button.setId(1000);  // The ID can be anything really 

LayoutParams textParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
textParams.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
textParams.addRule(RelativeLayout.ABOVE, button.getId()); 

LayoutParams buttonParams = new LayoutParams(LayoutParams.FILL_PARENT, 100); // I used a basic height of 100px here 
buttonParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 

text.setLayoutParams(textParams); 
button.setLayoutParams(buttonParams); 

layout.setLayoutParams(layoutParams); 

layout.addView(text); 
layout.addView(button); 

setContentView(layout); 
0

尝试是这样的:

<?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" > 

    <EditText 
     android:id="@+id/edittext" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:hint="Enter note here" 
     android:layout_weight="10" 
     android:gravity="center" 
     /> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Done" 
     android:layout_weight="1" 
     /> 

</LinearLayout>