Android按钮布局-5个按钮和一个徽章

Android按钮布局-5个按钮和一个徽章,android,android-layout,android-tablelayout,Android,Android Layout,Android Tablelayout,我花了很多时间试图完善我的xml设计,使它能够在手机和平板电脑上工作,包括水平方向和垂直方向 在前4项中,它是TableRows,最后一项是RelativeLayout,因为徽章,谁能帮我重新设计它,使它们看起来都一样,在手机和平板电脑上都很好看。在不使用固定宽度等的情况下,排列按钮的最佳方式是什么 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.androi

我花了很多时间试图完善我的xml设计,使它能够在手机和平板电脑上工作,包括水平方向和垂直方向

在前4项中,它是TableRows,最后一项是RelativeLayout,因为徽章,谁能帮我重新设计它,使它们看起来都一样,在手机和平板电脑上都很好看。在不使用固定宽度等的情况下,排列按钮的最佳方式是什么

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/backgroundColor"
    android:weightSum="5.3"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MenuActivity">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="30sp">

        <Space
            android:layout_width="0dp"
            android:layout_weight="0.10"
            android:layout_height="wrap_content" />

        <Button
            android:text="@string/painter"
            android:id="@+id/btn_painter"
            android:background="@drawable/painter_button_bg"
            android:textColor="@color/white"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.70"
            android:textStyle="normal|bold"
            android:textSize="40sp" />

        <Space
            android:layout_width="0dp"
            android:layout_weight="0.10"
            android:layout_height="wrap_content" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20sp">

        <Space
            android:layout_width="0dp"
            android:layout_weight="0.10"
            android:layout_height="wrap_content" />

        <Button
            android:text="@string/workman"
            android:id="@+id/btn_workman"
            android:background="@drawable/workman_button_bg"
            android:textColor="@color/white"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.70"
            android:textStyle="normal|bold"
            android:textSize="40sp" />

        <Space
            android:layout_width="0dp"
            android:layout_weight="0.10"
            android:layout_height="wrap_content" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20sp">

        <Space
            android:layout_width="0dp"
            android:layout_weight="0.10"
            android:layout_height="wrap_content" />

        <Button
            android:text="@string/vvs"
            android:id="@+id/btn_vvs"
            android:textColor="@color/white"
            android:background="@drawable/vvs_button_bg"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.70"
            android:textStyle="normal|bold"
            android:textSize="40sp" />

        <Space
            android:layout_width="0dp"
            android:layout_weight="0.10"
            android:layout_height="wrap_content" />
    </TableRow>

    <View
        android:layout_height="3dp"
        android:background="@android:color/darker_gray"
        android:layout_width="match_parent"
        android:layout_marginTop="10sp"
        android:layout_marginBottom="10sp"/>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center_horizontal">

        <Space
            android:layout_width="0dp"
            android:layout_weight="0.10"
            android:layout_height="wrap_content" />

        <Button
            android:text="@string/edit_list"
            android:id="@+id/btn_editlist"
            android:background="@drawable/edit_button_bg"
            android:textColor="@color/white"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.70"
            android:textStyle="normal|bold"
            android:textSize="40sp" />

        <Space
            android:layout_width="0dp"
            android:layout_weight="0.10"
            android:layout_height="wrap_content" />
    </TableRow>

    <View
        android:layout_height="3dp"
        android:background="@android:color/darker_gray"
        android:layout_width="match_parent"
        android:layout_marginTop="10sp"
        android:layout_marginBottom="10sp"/>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center_horizontal">

        <Space
            android:layout_width="0dp"
            android:layout_weight="0.10"
            android:layout_height="wrap_content" />

        <RelativeLayout
            android:id="@+id/badge_layout3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <RelativeLayout
                android:id="@+id/relative_layout_item_count2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <Button
                    android:id="@+id/btn_sendlist"
                    android:text="@string/send_list"
                    android:background="@drawable/send_button_bg"
                    android:textColor="@color/white"
                    android:layout_width="300dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.70"
                    android:textStyle="normal|bold"
                    android:textSize="40sp" />
            </RelativeLayout>

            <TextView
                android:id="@+id/badge_notification_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignRight="@id/relative_layout_item_count2"
                android:background="@android:drawable/ic_notification_overlay"
                android:text="5"
                android:textColor="#FFF"
                android:textSize="12sp"
                android:textStyle="bold" />
        </RelativeLayout>

        <Space
            android:layout_width="0dp"
            android:layout_weight="0.10"
            android:layout_height="wrap_content" />
    </TableRow>

</LinearLayout>

以下是一个您可以使用的示例。它是我的应用程序布局的直接副本,我有一个播放、暂停和停止按钮,它均匀地填充任何显示的整个宽度,无论大小`

<LinearLayout
    android:id="@+id/ll_buttonBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:orientation="horizontal"
    >
    <ImageButton
        android:id="@+id/ib_play"
        android:src="@drawable/ic_play"
        android:contentDescription="@string/play"
        android:background="@drawable/image_border"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        />
    <ImageButton
        android:id="@+id/ib_pause"
        android:src="@drawable/ic_pause"
        android:contentDescription="@string/pause"
        android:background="@drawable/image_border"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        />
    <ImageButton
        android:id="@+id/ib_stop"
        android:src="@drawable/ic_stop"
        android:contentDescription="@string/stop"
        android:background="@drawable/image_border"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        />
</LinearLayout>

如果需要换行符,可以在每个按钮之间添加此选项

<View
        android:layout_height="match_parent"
        android:layout_width="2dp"
        android:background="@color/background"
        />

1.)将其添加到gradle文件中
编译'com.wefika:flowlayout:0.4.1'
2) 将其添加到布局文件中
3) 将此添加到主活动中
公共类MainActivity扩展了AppCompatActivity{
编辑文本myEditText;
流程布局myFlowLayout;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myEditText=(EditText)findViewById(R.id.myEditText);
myFlowLayout=(FlowLayout)findViewById(R.id.myFlowLayout);
myEditText.setOnEditorActionListener(新的TextView.OnEditorActionListener(){
@凌驾
公共布尔onEditorAction(TextView v、int actionId、KeyEvent事件){
if(actionId==EditorInfo.IME\u ACTION\u DONE){
createBadget(myEditText.getText().toString().trim());
myEditText.setText(“”);
Toast.makeText(MainActivity.this,“DONE”,Toast.LENGTH_SHORT.show();
}
返回false;
}
});
}
私有void createBadget(字符串str){
FlowLayout.LayoutParams params=新的FlowLayout.LayoutParams(FlowLayout。
LayoutParams.WRAP_内容,FlowLayout.LayoutParams.WRAP_内容);
最终视图子对象=GetLayoutFlater()。充气(R.layout.text\u badget,null);
参数设置边距(14,14,14,14);
setLayoutParams(params);
最终文本视图电视=(文本视图)child.findviewbyd(R.id.badge\u电视);
tv.setText(str);
ArrayList mylist=新的ArrayList();
mylist.add(str);
myFlowLayout.addView(子级,myFlowLayout.getChildCount()-1);
System.out.println(“arraylist::”+mylist);
}
} 

这没有帮助:(
1.) add this in gradle file 
 compile 'com.wefika:flowlayout:0.4.1'

2) add this in layout file 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
  >

    <com.wefika.flowlayout.FlowLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/myFlowLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="start|top">
        <EditText
            android:id="@+id/myEditText"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:minWidth="100dp"
            android:layout_margin="10dp"
            android:paddingLeft="24dp"
            android:paddingRight="24dp"
            android:background="@drawable/edittext_bg"
            android:inputType="text"/>
    </com.wefika.flowlayout.FlowLayout>

</LinearLayout>

3) add this in main activity
public class MainActivity extends AppCompatActivity {

    EditText myEditText;

    FlowLayout myFlowLayout;

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

        myEditText = (EditText)findViewById(R.id.myEditText);

        myFlowLayout = (FlowLayout)findViewById(R.id.myFlowLayout);

        myEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId == EditorInfo.IME_ACTION_DONE){
                    createBadget(myEditText.getText().toString().trim());
                    myEditText.setText("");
                    Toast.makeText(MainActivity.this,"DONE",Toast.LENGTH_SHORT).show();
                }
                return false;
            }
        });
    }


    private void  createBadget(String str) {

        FlowLayout.LayoutParams params = new FlowLayout.LayoutParams(FlowLayout.
                LayoutParams.WRAP_CONTENT, FlowLayout.LayoutParams.WRAP_CONTENT);

        final View child = getLayoutInflater().inflate(R.layout.text_badget, null);

        params.setMargins(14, 14, 14, 14);

        child.setLayoutParams(params);

        final TextView tv = (TextView)child.findViewById(R.id.badge_tv);
        tv.setText(str);

        ArrayList<String> mylist = new ArrayList<String>();
        mylist.add(str);

        myFlowLayout.addView(child,myFlowLayout.getChildCount()-1);
        System.out.println("arraylist ::"+mylist);

    }
}