如何从右到左放置textView的垂直滚动条?

时间:2018-09-24 18:19:23

标签: java android android-layout android-studio textview

我在Android应用程序的textview中应用了垂直滚动条,但默认情况下显示在textview的右侧。我想将其移动(放置)到textView的左侧。这是代码,请告诉我们需要进行哪些修改:

TextView abc.setText("sfihogwhgo \n lwhgieowehgwegoihwgoe \n wfeliwegh \n woihgi");
 abc.setMovementMethod(new ScrollingMovementMethod());

此外,在布局xml中:

<TextView
                android:id="@+id/textView_ghazal"
                android:layout_width="match_parent"
                android:layout_height="514dp"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:lineSpacingExtra="20sp"
                android:textAlignment="center"
                android:textSize="30sp"
                android:textStyle="bold"
                android:scrollbars="vertical"
                />

1 个答案:

答案 0 :(得分:1)

有两种方法可以做到这一点,一种是通过xml:

class FollowUsersTableViewController: UITableViewController ,UISearchResultsUpdating, UISearchControllerDelegate{

//    @IBOutlet var followUsersTableView: UITableView!

func updateSearchResults(for searchController: UISearchController) {
    searchController.searchResultsController?.view.isHidden = false
    filterContent(searchText: self.searchController.searchBar.text!)
    self.tableView.reloadData()
}
private var viewIsHiddenObserver: NSKeyValueObservation?
let searchController = UISearchController(searchResultsController: nil)
var usersArray = [NSDictionary?]()
var filteredUsers = [NSDictionary?]()
var loggedInUser: User?

var databaseRef = Database.database().reference()

override func viewDidLoad() {
    super.viewDidLoad()
    //large title
    self.title = "Discover"
    if #available(iOS 11.0, *) {
        self.navigationController?.navigationBar.prefersLargeTitles = true
    } else {
        // Fallback on earlier versions
    }
    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false
    self.searchController.delegate = self;
    definesPresentationContext = true
    tableView.tableHeaderView = searchController.searchBar

    databaseRef.child("profile").queryOrdered(byChild: "username").observe(.childAdded, with: { (snapshot) in

        let key = snapshot.key
        let snapshot = snapshot.value as? NSDictionary
        snapshot?.setValue(key, forKey: "uid")

        if(key == self.loggedInUser?.uid) {
            print("Same as logged in user, so don't show!")
        } else {
            self.usersArray.append(snapshot)
            //insert the rows
            self.tableView.insertRows(at: [IndexPath(row:self.usersArray.count-1,section:0)], with: UITableViewRowAnimation.automatic)
            self.tableView.reloadData()
        } 
    }) { (error) in
        print(error.localizedDescription)
    }

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem
}

// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    if searchController.isActive && searchController.searchBar.text != ""{
        return filteredUsers.count
    }

    return self.usersArray.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! FollowTableViewCell

    let user : NSDictionary?

    if searchController.isActive && searchController.searchBar.text != ""{
        user = filteredUsers[indexPath.row]
    }
    else
    {
        user = self.usersArray[indexPath.row]
    }

    cell.title?.text = user?["username"] as? String
    let url = URL(string: user?["photoURL"] as! String)!
    cell.userImage?.sd_setImage(with: url, placeholderImage: #imageLiteral(resourceName: "user_male"), options: .progressiveDownload, completed: nil)
    return cell
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 50
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let arr = self.searchController.isActive ? self.filteredUsers : self.usersArray
    self.performSegue(withIdentifier: "user", sender: arr[indexPath.row])
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let dest = segue.destination as! UserProfileViewController
    let obj = sender as![String:Any]
    dest.selectedUser = obj
}

func filterContent(searchText:String)
{
    if searchText != ""{
        self.filteredUsers = self.usersArray.filter{ user in

            let username = user!["username"] as? String

            return(username?.lowercased().contains(searchText.lowercased()))! 
        }
    }
}

或通过Java代码:

android:verticalScrollbarPosition="left"