我有以下观点我想以编程方式将重力设置为ImageView
但不幸的是我无法做到这一点,我已经阅读了很多SO问题和答案但仍然无效
<?xml version="1.0" encoding="utf-8"?>
<com.project.widgets.CustomRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frontRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/listview_click"
android:padding="@dimen/watchlist_front_row_padding">
<LinearLayout
android:id="@+id/linearLayoutMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/imgDeleteStocks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingBottom="@dimen/watchlist_delete_img_selector_padding"
android:paddingRight="@dimen/watchlist_delete_img_selector_padding"
android:paddingTop="@dimen/watchlist_delete_img_selector_padding"
android:src="@drawable/img_watchlist_delete_checked"
android:visibility="visible" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0">
<com.project.widgets.TextViewRobotoLight
android:id="@+id/textViewCompanyName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.6"
android:singleLine="true"
android:text=""
android:textColor="@color/color_primary_text_color_fivep"
android:textSize="@dimen/watchlist_company_Name" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:clickable="false"
android:gravity="right">
<com.project.widgets.TextViewRobotoLight
android:id="@+id/textViewSecondColumn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/watchlist_company_last_rate_left_padding"
android:text=""
android:textColor="@color/color_primary_text_color_fivep"
android:textSize="@dimen/watchlist_price" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5">
<com.project.widgets.TextViewRobotoLight
android:id="@+id/textViewExchange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text=""
android:textColor="@color/color_time_background"
android:textSize="@dimen/watchlist_symbol" />
<com.project.widgets.TextViewRobotoLight
android:id="@+id/textViewFullName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textViewExchange"
android:text=""
android:textColor="@color/color_time_background"
android:textSize="@dimen/watchlist_symbol"
android:visibility="visible" />
<ImageView
android:id="@+id/imgfilter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textViewExchange"
android:layout_alignTop="@+id/textViewExchange"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/textViewExchange"
android:src="@drawable/icon_edit_watchlist"
android:textColor="@android:color/white" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/txtOpeningBracket"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingRight="3dp">
<ImageView
android:id="@+id/imgChangeIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/nifty_sensex_img_change_indicator_padding"
android:scaleType="fitEnd"
android:src="@drawable/img_negative_per_change" />
<com.project.widgets.TextViewRobotoLight
android:id="@+id/textViewThirdColumn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="@color/color_time_background"
android:textSize="@dimen/watchlist_symbol" />
</LinearLayout>
<com.project.widgets.TextViewRobotoLight
android:id="@+id/txtOpeningBracket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/textViewFourthColumn"
android:text="("
android:textColor="@color/color_time_background"
android:textSize="@dimen/watchlist_symbol" />
<com.project.widgets.TextViewRobotoLight
android:id="@+id/textViewFourthColumn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/txtClosingBracket"
android:clickable="false"
android:text=""
android:textColor="@color/color_time_background"
android:textSize="@dimen/watchlist_symbol" />
<com.project.widgets.TextViewRobotoLight
android:id="@+id/txtClosingBracket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text=")"
android:textColor="@color/color_time_background"
android:textSize="@dimen/watchlist_symbol" />
<com.project.widgets.TextViewRobotoLight
android:id="@+id/txtVolume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/txtClosingBracket"
android:text=""
android:textColor="@color/color_time_background"
android:textSize="@dimen/watchlist_symbol" />
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/nsebase_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:button="@drawable/selector_nse_bse_switch"
android:checked="true" />
<com.project.widgets.TextViewRobotoLight
android:id="@+id/textEquityCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:layout_toRightOf="@+id/nsebase_switch"
android:background="@color/color_market_depth_header"
android:paddingBottom="@dimen/watchlist_equity_category_top_bottom_padding"
android:paddingLeft="@dimen/watchlist_equity_category_left_right_padding"
android:paddingRight="@dimen/watchlist_equity_category_left_right_padding"
android:paddingTop="@dimen/watchlist_equity_category_top_bottom_padding"
android:text=""
android:textColor="@android:color/white" />
<com.project.widgets.TextViewRobotoLight
android:id="@+id/txtBuy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="@dimen/buy_sell_inbetween_margin"
android:layout_toLeftOf="@+id/txtSell"
android:background="@drawable/selector_watchlist_buy_btn"
android:paddingBottom="@dimen/buy_sell_top_bottom_padding"
android:paddingLeft="@dimen/buy_left_right_padding"
android:paddingRight="@dimen/buy_left_right_padding"
android:paddingTop="@dimen/buy_sell_top_bottom_padding"
android:text="@string/string_buy"
android:textColor="@color/color_primary_text_color_fivep" />
<com.project.widgets.TextViewRobotoLight
android:id="@+id/txtSell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/selector_watchlist_sell_btn"
android:paddingBottom="@dimen/buy_sell_top_bottom_padding"
android:paddingLeft="@dimen/sell_left_right_padding"
android:paddingRight="@dimen/sell_left_right_padding"
android:paddingTop="@dimen/buy_sell_top_bottom_padding"
android:text="@string/string_sell"
android:textColor="@color/color_primary_text_color_fivep" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</com.project.widgets.CustomRelativeLayout>
所以imgDeleteStocks
位于我LinearLayout
内的CustomRelativeLayout
内,所以我想将imgDeleteStocks
重力设置为TOP
请帮助我1} p>
答案 0 :(得分:1)
使用relativeLayout
包装ImageView <RelativeLayout
android:id="@+id/imgDeleteStocks_rl"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1" >
<ImageView
android:id="@+id/imgDeleteStocks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_launcher"
android:visibility="visible" />
</RelativeLayout>
添加程序代码
ImageView iv = (ImageView)findViewById(R.id.imgDeleteStocks);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)iv.getLayoutParams();
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
iv.setLayoutParams(lp);
答案 1 :(得分:0)
试试这个:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.TOP;
imgDeleteStocks.setLayoutParams(layoutParams);