以编程方式设置ImageButton layout_gravity

dum*_*ers 7 android android-layout android-linearlayout layout-gravity

我想做什么

我尝试通过Java代码设置我ImageButtonlayout_gravity,我想要ImageButton的方式就像下面图像中橙色框架内的方式一样:

图片

框是垂直LinearLayout充当"基地"布局,这是我想孩子布局添加到布局.

橙色红色都是水平LinearLayout.

橙色布局是我想要把路ImageButtonTextView,这一次是通过XML建立

的布局是我试图模仿结果橙色通过Java代码布局.

相关守则

这是设置Orange布局的XML代码,这是我想通过Java代码实现的效果:

        <!-- Begin the Orange Layout -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/info_left_padding"
            android:layout_marginRight="@dimen/info_right_padding" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:gravity="center_vertical"
                android:minHeight="@dimen/detail_min_line_item_height"
                android:text="TextView" />

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

                <ImageButton
                    android:id="@+id/imageButton1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right|center_vertical"
                    android:adjustViewBounds="true"
                    android:maxHeight="@dimen/abs__action_bar_default_height"
                    android:scaleType="fitCenter"
                    android:src="@drawable/navigation_cancel" />
            </FrameLayout>
        </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这是设置Red布局的Java代码

        int textHeight = (int)getResources().getDimension(R.dimen.detail_min_line_item_height);
        int imgHeight = (int)getResources().getDimension(R.dimen.abs__action_bar_default_height);

        TextView mTextView = new TextView(this);
        ImageButton mDeleteButton = new ImageButton(this);

        // Set Delete Button Padding
//      mDeleteButton.setPadding(buttonPadding, buttonPadding, buttonPadding, buttonPadding);

        // Set Imagebutton Scale type as fitCentre
        mDeleteButton.setScaleType(ScaleType.FIT_CENTER);

        // Set AdjustViewBounds
        mDeleteButton.setAdjustViewBounds(true);

        // Set max height of the image
        mDeleteButton.setMaxHeight(imgHeight);

        // Set the text appearance to be "large"
        mTextView.setTextAppearance(this, android.R.style.TextAppearance_Large);
        mTextView.setText(text);

        // Set the minimum height of this textview
        mTextView.setMinHeight(textHeight);

        // Set the content of the textview to be centred
        mTextView.setGravity(Gravity.CENTER_VERTICAL);

        // Set the ImageButton's background image
        mDeleteButton.setImageResource(R.drawable.navigation_cancel);

        LinearLayout.LayoutParams hParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

        LinearLayout hLayout = new LinearLayout(this);
        // Set Margins
        hParams.leftMargin = (int) getResources().getDimension(R.dimen.info_left_padding);
        hParams.rightMargin = (int) getResources().getDimension(R.dimen.info_right_padding);
        hParams.bottomMargin = (int) getResources().getDimension(R.dimen.text_layout_margin);

        hLayout.setLayoutParams(hParams);
        // Set orientation to horizontal
        hLayout.setOrientation(LinearLayout.HORIZONTAL);

        // The settings below is actually setting up some of the button's parameters        
        LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        buttonParams.gravity = Gravity.RIGHT;
        mDeleteButton.setLayoutParams(buttonParams);

        hLayout.addView(mTextView);
        hLayout.addView(mDeleteButton);

        layout_blue.addView(hLayout);
Run Code Online (Sandbox Code Playgroud)

我到目前为止所做的一切

根据这样的一些SO帖子:android的android方法:layout_gravity我最初尝试先把我ImageButton放入一个FrameLayout然后设置这个的params FrameLayout,如下所示:

FrameLayout buttonFrame = new FrameLayout(this);
FrameLayout.LayoutParams buttonParams = new FrameLayout.LayoutParams(android.widget.FrameLayout.LayoutParams.WRAP_CONTENT, 
                                                                            android.widget.FrameLayout.LayoutParams.WRAP_CONTENT, 
                                                                            Gravity.RIGHT | Gravity.CENTER_VERTICAL);
buttonFrame.setLayoutParams(buttonParams);
buttonFrame.addView(mDeleteButton);
Run Code Online (Sandbox Code Playgroud)

但是我的结果与上面提到的图像相同.后来我也尝试将LayoutParams宽度更改为MATCH_PARENT only to find theImageButton`水平拉伸(是的,它被拉伸)

然后我尝试了在这两个SO帖子中发布的方法:

如何以编程方式设置layout_gravity?

如何以编程方式设置按钮的参数

他们的方法是设置LinearLayout.Params第一个,然后将这个参数应用于按钮(我在相关代码部分中发布的代码应用此方法).简而言之就是:

    // The settings below is actually setting up some of the button's parameters        
    LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    buttonParams.gravity = Gravity.RIGHT;
    mDeleteButton.setLayoutParams(buttonParams);
Run Code Online (Sandbox Code Playgroud)

但是,结果仍然与上面显示的图像相同.

由于我需要以编程方式稍后向Blue布局添加更多子视图,我想知道是否有办法在图像中设置每个子布局,如橙色布局?

最后

我找到了一个与@Permita的答案非常相似的解决方案.

这是我的解决方案:

    LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT);
    textParams.weight = 1.0f;
    mTextView.setLayoutParams(textParams);
Run Code Online (Sandbox Code Playgroud)

小智 1

添加以下代码,它将把所有可用空间分配给 texView,将按钮移动到布局的右侧,使其看起来像 OrangeLayout。

mTextView.setLayoutParams(LayoutParams param = new LinearLayout.LayoutParams(
                         LayoutParams.MATCH_PARENT,
                         LayoutParams.MATCH_PARENT, 1.0f));
Run Code Online (Sandbox Code Playgroud)