【问题标题】:Android layout design that have two options depending on the data根据数据有两种选择的Android布局设计
【发布时间】:2021-04-08 02:21:32
【问题描述】:

我想显示 4 个 textViews 或只显示一个 editText,具体取决于我从后端收到的数据

以下是我的看法

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

  <LinearLayout
    android:id="@+id/groupOfFour" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

        <TextView
            android:id="@+id/first"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
          />
        <TextView
            android:id="@+id/second"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
        <TextView
            android:id="@+id/third"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            t/>
        <TextView
            android:id="@+id/fourth"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
   </LinearLayout>

   <EditText
       android:id="@+id/editText"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>
</LinearLayout>

在我的片段中,我正在这样做

class MyFragment : Fragment(R.layout.my_fragment) {

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
   val backedData = getRemoteData()
   if(backedData.hasFour()) {
     editText.visibility = Visibility.GONE
     // set the four text view
   } else {
     groupOfFour.visibility = Visibility.GONE
   }
  }
}

还有其他方法可以实现相同的行为吗?

【问题讨论】:

    标签: android android-layout kotlin android-fragments textview


    【解决方案1】:

    您可以使用addView 以编程方式添加视图

    class MyFragment : Fragment(R.layout.my_fragment) {
    
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
            
           val backedData = getRemoteData()
    
           if(backedData.hasFour()) {
                groupOfFour.addView(addTextView("Textview 1"))
             
            // set the four text view
           } else {
           
                groupOfFour.addView(addTextView("Textview 1"))
                groupOfFour.addView(addTextView("Textview 2"))
                groupOfFour.addView(addTextView("Textview 3"))
                groupOfFour.addView(addTextView("Textview 4"))
    
           }
        }
        
        fun addTextView(mytext: String):TextView {
            return TextView(activity).apply {
                layoutParams = LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
                )
                text = mytext
            }
        }  
    }
    

    并从布局中删除TextViews

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
      <LinearLayout
        android:id="@+id/groupOfFour" 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
       </LinearLayout>
    
       <EditText
           android:id="@+id/editText"
           android:layout_width="match_parent"
           android:layout_height="match_parent"/>
    </LinearLayout>
    

    如果要设置静态Id为TextViews,可以查看this answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 2021-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多