【问题标题】:Not able to set gravity to imageview programmatically无法以编程方式将重力设置为 imageview
【发布时间】:2016-08-08 14:32:02
【问题描述】:

我有以下观点,我想以编程方式将重力设置为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>

所以imgDeleteStocksLinearLayout 里面,在我的CustomRelativeLayout 里面,所以我想将imgDeleteStocks 重力设置为TOP 请帮助我

【问题讨论】:

  • 你能添加屏幕图像吗?
  • 你刚刚设置了android:layout_gravity="center_vertical",你把imageview设置在上面是什么意思?
  • @S.W.需要以编程方式设置它
  • 要以编程方式设置它,您可以使用一种棘手的方法通过 relativelayout 将 imageview 包装在线性布局内,并使用 RelativeLayout.LayoutParams 将您的 imageview 对齐到顶部
  • 我只是在问这个问题,但不确定如何给我正确的答案

标签: android layoutparams


【解决方案1】:
  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>
    
  2. 添加程序代码

    ImageView iv = (ImageView)findViewById(R.id.imgDeleteStocks);
    RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)iv.getLayoutParams();
    lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    iv.setLayoutParams(lp);
    

【讨论】:

  • 请注意,相对布局的layout_height必须是match_parent
  • 在您的xml代码中,您没有设置weightsum,因此,只需将android:layout_width="0dp"修改为android:layout_width="wrap_content"并删除android:layout_weight="1"
【解决方案2】:

试试这个:

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.TOP;
imgDeleteStocks.setLayoutParams(layoutParams);

【讨论】:

  • 只是建议,我会推荐使用 LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams ) imgDeleteStocks.getLayoutParams();而不是 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  • @Hello 它不工作..@S.W.伙计,请给我正确的答案男人
猜你喜欢
  • 2011-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-05
  • 2012-05-08
  • 1970-01-01
  • 2018-04-15
  • 1970-01-01
相关资源
最近更新 更多