如何将项目水平添加到导航抽屉菜单?

时间:2018-11-21 12:35:09

标签: android navigation-drawer

enter image description here

以上内容的行为类似于Android导航抽屉菜单,但使用c ++编写,我正尝试使用导航抽屉菜单在Android中复制它,但已垂直实现。这是电视应用程序的一部分,而不是智能手机。我似乎无法在水平显示内容,这可能吗?有人可以帮忙吗? 到目前为止,这是我所处理的;

enter image description here

activity_navigation_drawer.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:id="@+id/nav_drawer">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="1dp"
        android:contentDescription="Icons"/>

    <TextView
        android:id="@+id/titles"
        android:layout_width ="90dp"
        android:layout_height ="wrap_content"
        android:layout_marginLeft ="-40dp"
        android:layout_marginTop ="70dp"
        android:textColor ="@color/colorPrimary"
        android:textSize="16sp"
        android:layout_below ="@+id/icon"/>  
</LinearLayout>

NavigationAdapter.java

public class NavigationAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] titles;
    private final int[] icon;

    public NavigationAdapter(Context context, int[] icon, String[] titles){
        super(context, R.layout.activity_navigation_drawer,titles);
        this.context=context;
        this.icon = icon;
        this.titles = titles;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.activity_navigation_drawer,parent,false);

        ImageView imageView=(ImageView)rowView.findViewById(R.id.icon);
        TextView textView = (TextView)rowView.findViewById(R.id.titles);

        imageView.setImageResource(icon[position]);
        textView.setText(titles[position]);

        return rowView;
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private ListView mDrawerList1;
    private DrawerLayout mDrawerLayout;

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

        mDrawerList1 = (ListView) findViewById(R.id.resource_bank);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        addDrawerItems();

        navOne = (ImageButton) findViewById(R.id.nav_one);
        navOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
                    mDrawerLayout.closeDrawer(GravityCompat.START);
                } else {
                    mDrawerLayout.openDrawer(GravityCompat.START);
                }
            }
        });
    }

    private void addDrawerItems() {

        int [] icon = {R.drawable.xxx_my_computer,
                R.drawable.xxx_my_items,
                R.drawable.xxx_favorite,
                R.drawable.xxx_remote,
            };

        String [] name ={"Computer",
                "Items",
                "Favorite",
                "Remote",
            };

        NavigationAdapter onAdapter = new NavigationAdapter(MainActivity.this,icon,name);
        mDrawerList1.setAdapter(onAdapter);

        mDrawerList1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, "Resource Bank", Toast.LENGTH_SHORT).show();
            }
        });
    }       
}   

1 个答案:

答案 0 :(得分:0)

只需使用bottomnavigationview并使相对布局成为父布局,然后在bottomnavigation视图中简单使用alignparrenttop = true

制作一个水平方向的线性布局,然后给它权重值以选项,例如,如果要显示3选项,然后给它权重值3并简单地将其分为3个选项