2

第一张图片 第二张图片

我是新的android开发。下面我附上我的布局文件。

在我的布局中,我有一个和ListView一个。当我插入任务并按下该任务时,该任务将被一一添加。EditTextImageButtonEditTextImageButtonListView

我的问题是假设我添加了超过 4 个任务意味着视图不可见EditTextImageButton因为列表项占据了那个位置。我在上面添加了我的屏幕截图,请提供解决方案谢谢...

    `**Layout:**        
        <?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" >

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

        <Button
            android:id="@+id/todaytaskbtnid"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_weight="1"
            android:background="@drawable/buttonselector"
            android:text="TODAY" />

        <Button
            android:id="@+id/tomorrowtaskbtnid"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/buttonselector"
            android:text="TOMORROW" />

        <Button
            android:id="@+id/futuretaskbtnid"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/buttonselector"
            android:text="FUTURE" />
    </LinearLayout>

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

        <ListView
            android:id="@+id/frontpagetasklistid"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom" >

        <RelativeLayout
            android:id="@+id/relativelayoutid"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom" >

            <EditText
                android:id="@+id/edittextidAddTodayTask"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:background="@drawable/edittextselector"
                android:hint="Add Today Task" />

            <ImageButton
                android:id="@+id/imagebuttonidAddTodayTask"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:layout_gravity="right"
                android:src="@drawable/addtask" />
        </RelativeLayout>
    </LinearLayout>

</LinearLayout>
4

5 回答 5

2

你可以有一个Relative layout,或者你可以试试下面的。您可以拥有单一的相对布局并将小部件彼此相对放置。

另一种选择是将线性布局按钮小部件添加为标题。还将 editext 和 image 按钮作为页脚添加到列表视图。

    <Button
        android:id="@+id/todaytaskbtnid"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_weight="1"
        android:background="@drawable/ic_launcher"
        android:text="TODAY" />

    <Button
        android:id="@+id/tomorrowtaskbtnid"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/ic_launcher"
        android:text="TOMORROW" />

    <Button
        android:id="@+id/futuretaskbtnid"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/ic_launcher"
        android:text="FUTURE" />
</LinearLayout>
     <ListView
            android:id="@+id/frontpagetasklistid"
            android:layout_above="@+id/edittextidAddTodayTask"
            android:layout_below="@+id/ll"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

        </ListView>
        <EditText 
            android:id="@+id/edittextidAddTodayTask"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@drawable/ic_launcher"
            android:hint="Add Today Task"/>

        <ImageButton
            android:id="@+id/imagebuttonidAddTodayTask"
            android:layout_width="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:src="@drawable/ic_launcher" />
    </RelativeLayout>

编辑:使用单个相对布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/rl"
tools:context=".MainActivity" >


     <ListView
            android:id="@+id/frontpagetasklistid"
            android:layout_above="@+id/edittextidAddTodayTask"
            android:layout_below="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

        </ListView>

        <ImageButton
            android:id="@+id/imagebuttonidAddTodayTask"
            android:layout_width="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:src="@drawable/ic_launcher" />

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

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/button1"
            android:layout_centerHorizontal="true"
            android:text="Button" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/button2"
            android:layout_alignBottom="@+id/button2"
            android:layout_alignRight="@+id/frontpagetasklistid"
            android:layout_marginRight="11dp"
            android:text="Button" />

        <EditText
            android:id="@+id/edittextidAddTodayTask"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignRight="@+id/imagebuttonidAddTodayTask"

            android:ems="10"
            android:hint="Add Today Task" />

    </RelativeLayout>

图形编辑器屏幕截图

在此处输入图像描述

三星 Galaxy s3 屏幕截图

![在此处输入图像描述][2]

由于您通过添加更多项目来要求屏幕截图

![在此处输入图像描述][3]

![enter code here][1]

从讨论

有row.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="38dp"
        android:text="TextView" />

</RelativeLayout>

MainActivity.java

您可以将第一个 xml 用作 activity_main.xml

public class MainActivity extends Activity {
    EditText ed;
    ImageButton ib;
    ListView lv;
    ArrayList<String> aa;
    ArrayAdapter<String> arrayAdapter;
    CustomAdapter cus;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ed= (EditText) findViewById(R.id.edittextidAddTodayTask);
        ib= (ImageButton) findViewById(R.id.imagebuttonidAddTodayTask);
        aa = new ArrayList<String>();
        lv= (ListView) findViewById(R.id.frontpagetasklistid);
        for(int i=0;i<100;i++)
        {
            aa.add(""+i);
        }
        cus = new CustomAdapter();
         //arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, aa);
         lv.setAdapter(cus);
        ib.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Log.i("...............", "...............");
                aa.add(ed.getText().toString());
                cus.notifyDataSetChanged();

            }

        });
    }

   class CustomAdapter extends BaseAdapter
   {
       LayoutInflater mInflater;
       TextView tv;
        public CustomAdapter()
        {
            this.mInflater = LayoutInflater.from(MainActivity.this);  
        }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return aa.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public View getView( int arg0, View arg1, ViewGroup arg2) {

        if(arg1==null )
        {
             arg1=mInflater.inflate(R.layout.row, arg2,false);
        }
             tv= (TextView) arg1.findViewById(R.id.textView1);
             tv.setText(aa.get(arg0));


        return arg1;
        }

   }

}
于 2013-07-10T04:49:38.997 回答
1

嗨,请尝试使用重量进行列表视图

通过编辑您的 xml 的示例是:

<?xml version="1.0" encoding="utf-8"?>

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

    <Button
        android:id="@+id/todaytaskbtnid"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_weight="1"
        android:background="@drawable/buttonselector"
        android:text="TODAY" />

    <Button
        android:id="@+id/tomorrowtaskbtnid"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/buttonselector"
        android:text="TOMORROW" />

    <Button
        android:id="@+id/futuretaskbtnid"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/buttonselector"
        android:text="FUTURE" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/frontpagetasklistid"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:layout_weight="1">
    </ListView>

    <RelativeLayout
        android:id="@+id/relativelayoutid"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom" >

        <EditText
            android:id="@+id/edittextidAddTodayTask"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@drawable/edittextselector"
            android:hint="Add Today Task" />

        <ImageButton
            android:id="@+id/imagebuttonidAddTodayTask"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_gravity="right"
            android:src="@drawable/addtask" />
    </RelativeLayout>
</LinearLayout>

于 2013-07-10T05:02:55.997 回答
1

我对您的布局进行了一些更改:

 <?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" 
            android:weightSum="10">

            <LinearLayout 
             android:layout_width="fill_parent"
             android:layout_height="0dip"
             android:layout_weight="1"
             android:weightSum="30"
             android:orientation="horizontal" 
             >
             <Button
                 android:layout_height="fill_parent" 
                 android:layout_width="0dip"
                 android:layout_weight="10"
                 />
             <Button
                 android:layout_height="fill_parent" 
                 android:layout_width="0dip"
                 android:layout_weight="10"
                 />
             <Button
                 android:layout_height="fill_parent" 
                 android:layout_width="0dip"
                 android:layout_weight="10"
                 />
             </LinearLayout>
         <LinearLayout 
             android:layout_width="fill_parent"
             android:layout_height="0dip"
             android:layout_weight="8"
             >

            <ListView
                android:id="@+id/frontpagetasklistid"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

            </ListView>
            </LinearLayout>

            <LinearLayout 
             android:layout_width="fill_parent"
            android:layout_height="0dip"
             android:layout_weight="1"
             android:weightSum="10"
             android:orientation="horizontal" 
             >

            <LinearLayout 
             android:layout_width="fill_parent"
            android:layout_height="0dip"
             android:layout_weight="1"

             android:orientation="horizontal" 
             >

            <EditText 
                android:id="@+id/edittextidAddTodayTask"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:hint="Add Today Task"
                android:layout_weight="10"
                android:drawableRight="@drawable/ic_launcher"
                android:background="@android:color/black"
                />
   /// I remove the button . there is no need of it.
            </LinearLayout>


        </LinearLayout>

在此处输入图像描述

现在调用你的 drawableRight 点击监听器来处理动作

yourEditText.setOnTouchListener(new RightDrawableOnTouchListener(keyword) {
        @Override
        public boolean onDrawableTouch(final MotionEvent event) {
            // do whatever you want
        }
    });
于 2013-07-10T04:51:50.953 回答
1

您可以使用 listView.addFooterView(view); 这里的视图是指包含编辑文本和添加按钮的视图。

于 2013-07-10T04:52:26.993 回答
1

您可以将页脚视图添加到列表视图并使其不可见。在页脚视图中,复制底部栏的布局,其中包括 EditText 和 ImageButton。只需创建一个新的布局文件并将此 xml 粘贴到文件中。

 <RelativeLayout
                android:id="@+id/relativelayoutid"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:visibility="invisible" >

                <EditText
                    android:id="@+id/edittextidAddTodayTask"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/edittextselector"
                    android:hint="Add Today Task" />

                <ImageButton
                    android:id="@+id/imagebuttonidAddTodayTask"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentRight="true"
                    android:layout_gravity="right"
                    android:src="@drawable/addtask" />
            </RelativeLayout>

将此布局添加为 Listview 中的页脚视图。这样,您的 Listview 将永远不会隐藏原始底栏。不要忘记让它不可见。

于 2013-07-10T04:58:43.230 回答