【发布时间】:2013-03-31 19:11:15
【问题描述】:
我想做什么
我尝试通过 Java 代码设置我的 ImageButton 的 layout_gravity,我希望 ImageButton 的呈现方式如下图 Orange 框架内的方式:
蓝色框架是一个垂直的LinearLayout 充当“基础”布局,这是我尝试添加子布局的布局。
橙色和红色都是水平的LinearLayout。
Orange布局是我想放置ImageButton和TextView的方式,这个是通过XML设置的
Red 布局是我尝试通过 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>
这是设置 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);
到目前为止我已经尝试过什么
根据类似这样的一些 SO 帖子: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_PARENTonly to find theImageButton`被水平拉伸(是的,它被拉伸了)
然后我尝试了这两个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 布局,我想知道是否有一种方法可以设置每个子布局,如 Orange 中的图片?
终于
我找到了一个与@Permita 的答案非常相似的解决方案。
这是我的解决方案:
LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT);
textParams.weight = 1.0f;
mTextView.setLayoutParams(textParams);
【问题讨论】:
-
+1 提出好问题。
标签: android android-layout android-linearlayout layout-gravity