如何设置滚动视图内并包含两个 TextView 的线性布局的可见性消失?
Posted
技术标签:
【中文标题】如何设置滚动视图内并包含两个 TextView 的线性布局的可见性消失?【英文标题】:How to set visibility GONE of a Linear Layout which is inside a scroll view and contains two TextViews inside? 【发布时间】:2021-08-26 17:17:17 【问题描述】:主布局是一个线性布局,里面有一个包含子布局的滚动视图。这是我的布局[除了特定布局(用红色标记)之外的所有内容都省略了,因为它会很长]:
<ScrollView
android:layout_
android:layout_>
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_
android:layout_
android:layout_gravity="center"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="8dp"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="8dp"
app:cardElevation="10dp">
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical"
android:padding="8dp">
<LinearLayout
android:id="@+id/layoutIncomeTax"
android:layout_
android:layout_
android:orientation="horizontal">
<TextView
android:layout_
android:layout_
android:gravity="start"
android:text="Income Tax:"
android:textColor="@color/black"
android:textSize="16sp" />
<TextView
android:id="@+id/tvIncomeTax"
android:layout_
android:layout_
android:layout_weight="1"
android:gravity="end"
android:text="0"
android:textColor="@color/black"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</ScrollView>
这是我的代码(删除了不必要的代码):
公共类 ViewSalary 扩展 AppCompatActivity
private Spinner selectShift, selectYear, selectMonth;
private EditText edtEmployeeCode;
private Button viewSalaryBtn;
private String shift, year, month;
DatabaseReference rootDatabaseRef;
private LinearLayout layoutIncomeTax;
private TextView tvIncomeTax;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_salary);
viewSalaryBtn = findViewById(R.id.viewSalaryBtn);
layoutIncomeTax = findViewById(R.id.layoutIncomeTax);
tvIncomeTax = findViewById(R.id.tvIncomeTax);
rootDatabaseRef = FirebaseDatabase.getInstance().getReference().child("Salary");
viewSalaryBtn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
viewSalary();
String checkIncomeTax = tvIncomeTax.getText().toString();
if (checkIncomeTax.equals("0.0"))
layoutIncomeTax.setVisibility(layoutIncomeTax.GONE);
);
private void viewSalary()
final String empCode = edtEmployeeCode.getText().toString();
DatabaseReference empRef = rootDatabaseRef.child(shift).child(year).child(month).child(empCode);
empRef.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
if (dataSnapshot.exists())
String incomeTax = dataSnapshot.child("IncomeTax").getValue(String.class);
tvIncomeTax.setText(incomeTax);
else
Toast.makeText(ViewSalary.this, "Data does not exist!", Toast.LENGTH_SHORT).show();
@Override
public void onCancelled(@NonNull DatabaseError error)
Toast.makeText(ViewSalary.this, error.getMessage(), Toast.LENGTH_SHORT).show();
);
我想在加载数据后隐藏按钮单击时的所有线性布局,如果 TextView 值为“0.0”(如屏幕截图中标记为红色的那个)
Screenshot
【问题讨论】:
你试过 layoutIncomeTax.setVisibility(View.GONE); – Saravanan 刚刚编辑 @Saravanan 是的.. 我也试过了 您确定 checkIncomeTax.equals("0.0") 的评估结果为 true 吗? @Saravanan 请参考截图一次。我认为它的评估是真实的.. 【参考方案1】:在侦听器内部执行检查。
private Spinner selectShift, selectYear, selectMonth;
private EditText edtEmployeeCode;
private Button viewSalaryBtn;
private String shift, year, month;
DatabaseReference rootDatabaseRef;
private LinearLayout layoutIncomeTax;
private TextView tvIncomeTax;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_salary);
viewSalaryBtn = findViewById(R.id.viewSalaryBtn);
layoutIncomeTax = findViewById(R.id.layoutIncomeTax);
tvIncomeTax = findViewById(R.id.tvIncomeTax);
rootDatabaseRef = FirebaseDatabase.getInstance().getReference().child("Salary");
viewSalaryBtn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
viewSalary();
// Do not check here
);
private void viewSalary()
final String empCode = edtEmployeeCode.getText().toString();
DatabaseReference empRef = rootDatabaseRef.child(shift).child(year).child(month).child(empCode);
empRef.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
if (dataSnapshot.exists())
String incomeTax = dataSnapshot.child("IncomeTax").getValue(String.class);
tvIncomeTax.setText(incomeTax);
// Check here
if (incomeTax.equals("0.0"))
layoutIncomeTax.setVisibility(layoutIncomeTax.GONE);
else
Toast.makeText(ViewSalary.this, "Data does not exist!", Toast.LENGTH_SHORT).show();
@Override
public void onCancelled(@NonNull DatabaseError error)
Toast.makeText(ViewSalary.this, error.getMessage(), Toast.LENGTH_SHORT).show();
);
【讨论】:
【参考方案2】:试试这个。
//use trim method to remove spaces in string
String checkIncomeTax = tvIncomeTax.getText().toString().trim();
//hope you are not getting exactly 0.0.use log.i to see value then call if
log.i("checkIncomeTax",checkIncomeTax);
if (checkIncomeTax.equals("0.0"))
layoutIncomeTax.setVisibility(layoutIncomeTax.GONE);
【讨论】:
我也试过这个整数值..但仍然没有得到结果以上是关于如何设置滚动视图内并包含两个 TextView 的线性布局的可见性消失?的主要内容,如果未能解决你的问题,请参考以下文章
XCODE/Swift Autolayout ScrollView with dynamic label, textview etc. 如何根据 textview 设置滚动视图高度
如果要求是 Textview 应该显示最多 4 行的完整文本,那么如何设置 textview 的高度,然后它应该在 swift 3 中滚动?