如何正确设置手机屏幕内容;将屏幕分成多个视图

时间:2018-11-09 11:06:53

标签: android view fragment

我有1个工具栏,并在1个活动中列出了视图。片段中有1个按钮。但是工具栏与列表的起始区域重叠。按钮显示在底部屏幕的中间。但是列表项显示在按钮的水平面上,这是我不想要的。我的代码如下-

public class RoomShareFragment extends Fragment {
    private Button button;
    View retView;
    public RoomShareFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        retView = inflater.inflate(R.layout.fragment_room_share, container, false);
        button = retView.findViewById(R.id.button1);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        ((Activity)retView.getContext()).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int height = displayMetrics.heightPixels;
        int width = displayMetrics.widthPixels;
        button.setX(width/2-100);
        button.setY(height-450);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Code here executes on main thread after user presses button
            }
        });
        return retView;
    }
}

public class RoomShare extends AppCompatActivity {

    DB2CursorAdapter DBAdapter;
    MessDatabase mdb;
    Cursor cursor;
    ListView listview;
    String s = "hi";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room_share);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        String selectQuery = "SELECT  * FROM " + TempDataFromDB.TABLE_NAME /*+ " ORDER BY " + TempDataFromDB.COLUMN_MESS_NAME + " DESC"*/;
        this.deleteDatabase("ShareRoom_db");
        mdb = new MessDatabase(this);
        SQLiteDatabase db = mdb.getWritableDatabase();
        cursor = db.rawQuery(selectQuery,null);
        DBAdapter = new DB2CursorAdapter(this, cursor, 0);
            listview = (ListView) findViewById(R.id.listview);
            listview.setAdapter(DBAdapter);
            DBAdapter.notifyDataSetChanged();
    }

fragment_room_share.xml

   <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".RoomShareFragment"
        tools:showIn="@layout/activity_room_share">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:textColor="#ffffff"
            android:background="@drawable/draw"
            android:text="Buttons" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RoomShare!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </android.support.constraint.ConstraintLayout>

activity_room_share.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".RoomShare">

   <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <ListView android:id="@+id/listview"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_weight="5"/>

    <include layout="@layout/content_room_share" />



</android.support.design.widget.CoordinatorLayout>

0 个答案:

没有答案