【问题标题】:Android switch item position programmatically in a RelativeLayoutAndroid以编程方式在RelativeLayout中切换项目位置
【发布时间】:2019-08-26 19:21:52
【问题描述】:

我有一个RelativeLayout 显示一个应用程序名称及其图标。我希望能够从 java 端切换图标和文本位置,因此应用程序名称显示在图标下方,而无需膨胀新视图。这怎么可能?

这是我的代码

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:paddingBottom="27dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/appIcon"
        android:layout_width="54dp"
        android:layout_height="54dp"
        android:layout_margin="2dp"
        android:layout_below="@+id/appName"
        android:layout_centerHorizontal="true"
        android:contentDescription="icon"
        android:src="#80000000" />

    <TextView
        android:id="@+id/appName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:fontFamily="sans-serif-condensed"
        android:gravity="center"
        android:singleLine="true"
        android:text="name"
        android:textSize="13dp"/>

</RelativeLayout>

【问题讨论】:

    标签: java android android-relativelayout android-layoutparams


    【解决方案1】:

    您可以通过编程方式更改layout_below 规则,如下所示:

    TextView tvAppName = findviewById(R.id.appName)
    
    RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); 
        params.addRule(RelativeLayout.BELOW, R.id.appIcon);
        tvAppName .setLayoutParams(params);
    

    【讨论】:

      猜你喜欢
      • 2011-04-28
      • 1970-01-01
      • 2012-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多