gpt4 book ai didi

android - 以编程方式设置 ImageButton layout_gravity

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:12:42 26 4
gpt4 key购买 nike

我想做什么

我试图通过 Java 代码设置我的 ImageButtonlayout_gravity,我希望 ImageButton 的显示方式如下在下图中的橙色框内:

image

Blue 框架是一个垂直的 LinearLayout 作为“基础”布局,这是我尝试添加子布局的布局。

OrangeRed 都是水平的LinearLayout

Orange 布局是我想要放置 ImageButtonTextView 的方式,这个是通过 XML 设置的

Red 布局是我尝试通过 Java 代码模仿Orange 布局的结果。

相关代码

下面是设置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>

这是设置红色布局的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);

到目前为止我尝试了什么

根据一些这样的帖子:Java method for android:layout_gravity我最初尝试先将我的 ImageButton 放入 FrameLayout,然后设置此 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);

但我得到的结果与上面显示的图像相同。后来我也尝试将 LayoutParams 宽度更改为 MATCH_PARENT 才发现 ImageButton` 被水平拉伸(stretch)了(是的,它被拉伸(stretch)了)

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

How to set layout_gravity programmatically?

How to set a button's parameters programatically

他们的方法是先设置一个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);

但是,结果还是和上图一样。

问题

由于稍后我需要以编程方式向Blue 布局添加更多 subview ,我想知道是否有一种方法可以像 Orange 那样设置每个子布局图片?

最后

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

这是我的解决方案:

    LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT);
textParams.weight = 1.0f;
mTextView.setLayoutParams(textParams);

最佳答案

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

mTextView.setLayoutParams(LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f));

关于android - 以编程方式设置 ImageButton layout_gravity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15900523/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com