0

我有一个框架布局,我有一个列表和编辑文本和两个按钮,它的XML是:的Android XML布局

<?xml version="1.0" encoding="utf-8"?> 
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

     <RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="86dp" 
    android:layout_gravity="bottom" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:text="Send" /> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_toLeftOf="@+id/button1" 
     android:ems="10" /> 

    <Button 
     android:id="@+id/button2" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/button1" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:text="Button" /> 

    </RelativeLayout> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="317dp" > 

    </ListView> 

     </FrameLayout> 

和布局是这样的: enter image description here

我想把按钮名称也与列表视图上方的“按钮”,然后我应该做什么以实现这?

谢谢,关心塔利布。

回答

1

我不确定你是否真的需要FrameLayout,但根据你的要求,这可能更有效率。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <Button 
     android:id="@+id/button2" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:text="Button" /> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/button1" 
     android:layout_below="@+id/button2" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:text="Send" /> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_toLeftOf="@+id/button1" /> 

</RelativeLayout> 
+0

同时运行的代码编辑短信给我的错误,为什么这样? – Talib

+0

log cat是:10-27 23:58:14.559:E/AndroidRuntime(12056):java.lang.RuntimeException:无法启动活动ComponentInfo {soft.b.peopleassist/soft.b.peopleassist.MainActivity}:java。 lang.ClassCastException:android.widget.Button不能转换为android.widget.EditText – Talib

+0

您是否尝试将'Button'强制转换为'EditText'?确保在声明变量时,它们具有与布局中声明的相同的窗口小部件类型。 'Button btSend =(Button)findViewById(R.id.button1);','EditText et1 =(EditText)findViewById(R.id.editText1);'。 –

2

我可能会改为使用的LinearLayout:

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

    <Button 
     android:id="@+id/button2" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 
    </ListView> 

    <LinearLayout 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:orientation="horizontal"> 

     <EditText 
      android:id="@+id/editText1" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="wrap_content" 
      android:layout_toLeftOf="@+id/button1" 
      android:ems="10" /> 

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

</LinearLayout> 

具有输出是这样的:

enter image description here

+0

当我运行它它给我NPE在主要活动 – Talib

+0

logcat是:10-27 23:59:48.334:E/AndroidRuntime(12945):java.lang.RuntimeException:无法启动活动ComponentInfo {soft.b.peopleassist/soft.b.peopleassist.MainActivity}:java.lang.NullPointerException – Talib

+0

你可以发布整个logcat吗? (将其添加到问题中) – Jakob

1
// try this 
<?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"> 

    <Button 
     android:id="@+id/button2" 
     style="?android:buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 
    </ListView> 

    <LinearLayout 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:orientation="horizontal"> 

     <EditText 
      android:id="@+id/editText1" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="wrap_content" 
      android:layout_toLeftOf="@+id/button1" 
      android:ems="10" /> 

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

</LinearLayout> 

public class MainActivity extends ListActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

    } 

}