android - 以编程方式无法在 RelativeLayout 中对齐 ImageView

标签 android android-layout

我在将 ImageView 添加到相关布局时遇到问题。我想将图像添加到我使用 RelativeLayout 动态创建的菜单项列表中。我的所有菜单项都按顺序显示得很好,但是当我尝试向每个项目添加图像时,我只得到一个箭头,而且它没有垂直居中。下面是我的代码。

在我的 XML 文件中

<RelativeLayout 
            android:id="@+id/pMenu"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        </RelativeLayout>

在我的代码中:

private void buildMenu(String name, int id) {

    String[] menuItems = getResources().getStringArray(pMenus[id]);
    // Get the rel layout from xml
    RelativeLayout container = (RelativeLayout) findViewById(R.id.pMenu);

    int imageId=1;
    Typeface tf = Typeface.createFromAsset(this.getAssets(),"mreavesmodot-reg.otf");
    for(String menuItem: menuItems) {           

        // Defining the layout parameters
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);


        StyledButton menuImage = new StyledButton(this);
        menuImage.setBackgroundResource(R.drawable.menu_button);
        menuImage.setText(menuItem);
        menuImage.setTypeface(tf);
        menuImage.setTextSize(19);
        menuImage.setPadding(20, 0, 0, 0);
        menuImage.setTextColor(Color.WHITE);
        menuImage.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
        menuImage.setOnClickListener(getOnClickListener(menuImage, name));
        menuImage.setId(imageId);

        if(imageId==1) {
            lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        } else {
            lp.addRule(RelativeLayout.BELOW ,imageId-1);
        }
        menuImage.setLayoutParams(lp);


        ImageView arrow = new ImageView(this);
        arrow.setImageResource(R.drawable.arrow_menu);
        arrow.setPadding(0, 0, 15, 0);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, 
                RelativeLayout.LayoutParams.WRAP_CONTENT );
        params.addRule(RelativeLayout.ALIGN_RIGHT,menuImage.getId());
        params.addRule(RelativeLayout.CENTER_VERTICAL);

        arrow.setLayoutParams(params);

        container.addView(menuImage);
        container.addView(arrow);

        imageId++;
    }
}

最佳答案

我认为下面这行是你的问题

params.addRule(RelativeLayout.CENTER_VERTICAL); 

是的,您很可能添加了多个箭头,它们只是一个简单地叠加在一起,全部对齐到完整相对布局的垂直中心。该命令不是针对您的按钮项目执行垂直居中,而是针对父 RelativeLayout。

关于android - 以编程方式无法在 RelativeLayout 中对齐 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10973730/

相关文章:

android - 为什么我的 ViewPager 会在 CoordinatorLayout 中越界?

android - 我可以在自定义 ViewGroup 中获取 onBackPressed 事件吗?

android - 布局 xml 文件中的 "Unbound prefix"错误

android - CoordinatorLayout 用 subview 折叠 LinearLayout

Android:我可以使用 Design Support Library 而不是 Material Design UI 的 AppCompat 吗?

java - Android Studio : onTouchEvent either doesn't run or can't update variables

Android 应用程序 : Change Price on Google Play Developer Console (Alpha, 测试版)

android - 无法在 Recyclerview 下方添加空 View

android - Android 版 Google Map SDK 中标记周围的 map 不可点击

android - 如何在 Android Studio 中打开主题编辑器?